LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

What does ATTR_MAX_ENTRY_CHARS and ATTR_MAX_ENTRY_LENGTH attribute precisely for a textbox?

Hello, I'm using a textbox control and thought I could automatically limit the length of the control text with setting the ATTR_MAX_ENTRY_CHARS correctly.
#define ITEM_TXT_MAXLEN 80
SetCtrlAttribute (EditPanel, ACTIONEDIT_DATA, ATTR_MAX_ENTRY_CHARS, ITEM_TXT_MAXLEN-1);

I thought this would permit to avoid fatal errors when getting the control value:
char Item_Txt[ITEM_TXT_MAXLEN];
GetCtrlVal (EditPanel, ACTIONEDIT_DATA, Item_Txt);

But no, I still get following error:
FATAL RUN-TIME ERROR: "test3.c", line 4902, col 65, thread id 0x00000648: Array argument too small (80 bytes). Argument must contain at least 83 bytes (83 elements).

So, what do I do wrong? What does ATTR_MAX_ENTRY_CHARS precisely?
0 Kudos
Message 1 of 7
(3,524 Views)
Sorry I can't duplicate your problem in CVI 6.0. What version are you running?
See the attached example. I set the max chars based on a copy of your code and using malloc so you can change it at runtime.
What are the contents when you get the error? Could it be that you have some dual-byte characters in the textbox when you try to read the value? The help for ATTR_MAX_ENTRY_CHARS says that a dual-byte character counts as 1.
0 Kudos
Message 2 of 7
(3,511 Views)
Hello, I'm working with CVI 7.0.0.

I also have no problem with your code.
The uir works fine, but the ATTR_MAX_ENTRY_CHARS is not respected while adding programmatically chars to the control.
I extended your code.
Do following steps to reproduce the mentionned kind of error:
1. Click on the "malloc..." button
2. Use about 10 times the "Append" button.

I know I could check for the lengths in the sourcecode before writing to the control,
but that is exactly what the ATTR_MAX_ENTRY_CHARS is supposed to manage...
0 Kudos
Message 3 of 7
(3,508 Views)
Those two attributes are intended for the same purpose, which is to limit the amount of text that can be entered interactively in the text box (but keep in mind, that you can still enter text programmatically, using SetCtrlVal, that is longer than those limits).

The reason there are two attributes is because for one you specify the limit in bytes (MAX_ENTRY_LENGTH) and for the other you specify the limit in characters (MAX_ENTRY_CHARS). In the ANSI character set, bytes and characters are the same, but there are character sets such as Japanese and Korean, in which a single character can be 1 or 2 bytes.

Now, about that error you're having...

Since you set the limit to 79 characters, and assuming that you're running this code in a non-multibyte system, an 80-byte array should be sufficient to hold the text (you do need an extra byte for the NULL character). The obvious question is, how many characters are actually visible in the textbox at the time you call GetCtrlVal? Are there 79 characters in there, or 82 (including line feeds)? If there are more than 79 characters, how did the text get in there? Was it typed in, or was it set programmatically? Also, if instead of setting ATTR_MAX_ENTRY_CHARS to 79, you were to set ATTR_MAX_ENTRY_LENGTH to 79, would that change anything?

Luis
NI
Message 4 of 7
(3,504 Views)
I've now read your second post, and I realize that you are entering the text programmatically. Those attributes are intended to limit the end-user from entering too much text; they are not intended to limit you, the developer, from doing as you wish with the textbox. So, my suggestion is to always check the length and then allocate the memory dynamically. If it's a hassle to allocate memory dynamically, for error handling reasons for example, you have an alternative, which is to enforce the 79-character limit everywhere you set the text of the control programmatically. You can use the strncpy function for this purpose.

Luis
NI
0 Kudos
Message 5 of 7
(3,500 Views)
Thank you Luis,
that's exactly what I wanted to know.

These kind of things are not mentionned anywhere in the attribute description and LW/CVI help...
0 Kudos
Message 6 of 7
(3,493 Views)
I did duplicate your problem in 6.0 using your Append function. It appears that ATTR_MAX_ENTRY_CHARS only applies to text entered at the keyboard. It appears to be ignored when writing text using SetCtrlVal.
To avoid your runtime error, you can check the text length before GetCtrlVal using GetCtrlAttribute on ATTR_STRING_TEXT_LENGTH.
0 Kudos
Message 7 of 7
(3,490 Views)