LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Textbox GetNumTextBoxLines and GetTextBoxLine don't work right

Hello,

i think i found a bug in the textbox function.
After adding one line to a to an empty textbox GetNumTextboxLines returns 2 lines. After adding more there's always one line too much. Reading this line results an empty string.
Please see attachment, which is a enhanced version of the NI textbox example.
0 Kudos
Message 1 of 5
(4,544 Views)
If the line you add ends with a CarriageReturn/LineFeed (\n), you're also adding a blank line. This behavior is similar to any text editor or word processor: if a lines ends in a CarriageReturn/LineFeed, you can cursor down to the next line. The line is there, but it's empty until you add some text.
0 Kudos
Message 2 of 5
(4,527 Views)
Hi Al,

the CR's are not responsible. I attach an example.
0 Kudos
Message 3 of 5
(4,511 Views)
Hi,
I've run your example, and you seems to be right.... I also tried to insert null ('\0') characters at the end, without positive results!

I've almost done the same things you're doing with a List Box instead of a Text Box. It seems to work properly! In fact, with a list Box you have almost the same functions, but the number of list items matches....

Maybe a TextBox is sistematically filled with a CR...

Bye!
0 Kudos
Message 4 of 5
(4,501 Views)
I looked at your example, thanks.
The problem is not in GetNumTextBoxLines(), the problem is in InsertTextBoxLine(). A textbox accepts any text and isn't really organized into lines. All of the ???TextBoxLine functions try to interpret the text as divided into lines based on NewLines ('\n'). InsertTextBoxLine() automatically adds a NewLine character at the end of the string you're adding. You can verify this by doing an InsertTextBoxLine, then do a GetCtrlVal and look at the string. You'll see that it ends in a NewLine 0xa.
If you're inserting a line into the middle of the multi-line string in the textbox, InsertTextBoxLine is still handy. Using it in the middle of the string is not where you see the problem (because you need a NewLine there). Appending a line to the end of the string is where you get the extra blank line.
Here are a couple of suggestions for avoiding the blank line at the end of the textbox.
1. If you're just appending lines, don't use InsertTextBoxLine. Use SetCtrlVal and prefix the string with a NewLine (\n). (SetCtrlVal to a textbox appends to the existing contents).
e.g.
char myNewLine[256];
int myAge=99;
//...
sprintf(myNewLine, "\nMy age is %d", myAge);
SetCtrlVal(panel, PANEL_ACTIONBOX, myNewLine);

2. If you want to use InsertTextBoxLine, check to see if you're adding a line at the end of the textbox. If you are, trim the NewLine character.
e.g.
char textboxString[2000];
InsertTextBoxLine (panel, PANEL_ACTIONBOX, -1, str);
GetCtrlVal (panel, PANEL_ACTIONBOX, textboxString);
// strip off NewLine
if (textboxString[strlen(textboxString)-1] == '\n')
textboxString[strlen(textboxString)-1]='\0';
// rewrite the modified string without the ending newline
ResetTextBox (panel, PANEL_ACTIONBOX, textboxString);
0 Kudos
Message 5 of 5
(4,493 Views)