LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem With GetCtrlVal and a text box

I am trying to get the text entered in a text box with this code:

char com;

GetCtrlVal(panel, SAVEPANEL_TEXTBOX, &com);


the problem is when the code runs, I get an error saying "Argument too small.", referring to com. If I try to use a char array instead of just a char I get the "Found 'pointer to cell 100 of char', expected 'pointer to char'". any help would be appreciated.

Derek
0 Kudos
Message 1 of 5
(7,504 Views)
Hello Derek,

You have two options:

1. Delare your com variable as a character pointer.

char * com;

2. Use the GetTextBoxLine function to read a specific line of the text box or iterate through all the lines.

char text[MAX_LINE_LEN];
int i, numLines;

GetNumTextBoxLines (panelHandle, PANEL_TEXTBOX, &numLines);
for (i = 0; i < numLines; i++) {
GetTextBoxLine (panelHandle, PANEL_TEXTBOX, i, text);
}

Hope that helps.
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 2 of 5
(7,496 Views)
The problem is in the use of pointers to a string. GetCtrlVal is looking for a pointer. You want to read a string, not just a single character.
In your example, com is a single character. &com is a pointer to a single character.
If you make com a string (or an array of characters), don't use a & for the pointer.

// myChar is a single character
// &myChar is a pointer to the character
char myChar;
// myString is an array of characters
// myString is a pointer to the array
// &myString is Not a pointer to the array
char myString[256];

The compiler sees &myString as a pointer to a single character.
myString is a pointer to an array of characters (or a string).
&myString[0] is equivalent to myString used as a pointer.

You can't do a GetCtrlVal on a textbox to a single character (unless the textbox is empty). You get the "Argument too small." error you already saw. You need to GetCtrlVal to an array of characters.

You can read the entire textbox in one command. You may choose to read it one line at a time as in Wendy's example, but you don't need to.

char myString[256];
// incorrect call: the & is wrong here.
// GetCtrlVal (panelHandle, PANEL_TEXTBOX, &myString);
// correct call
GetCtrlVal (panelHandle, PANEL_TEXTBOX, myString);
// equivalent to correct call, but unneeded complication of syntax
// GetCtrlVal (panelHandle, PANEL_TEXTBOX, &myString[0]);

After the correct call to GetCtrlVal, myString will hold the entire contents of the textbox, even if it contains multiple lines. You need to make sure that myString is sized correctly.
0 Kudos
Message 3 of 5
(7,481 Views)
I just want to add to what Al wrote that if you don't know ahead of time how many characters the textbox might be holding, there is an attribute you can use to find out. In that scenario, your code would look something like this:

char *myString = NULL;


GetCtrlAttribute (panelHandle, PANEL_TEXTBOX, ATTR_STRING_TEXT_LENGTH, &numBytes);
nullChk(myString = malloc (numBytes + 1)); // +1 because of the NUL character at the end
GetCtrlVal (panelHandle, PANEL_TEXTBOX, myString);

... // use myString


if (myString)
free (myString);


Luis
NI
Message 4 of 5
(7,473 Views)
thanks for all the help.. sometimes my brain doesnt function properly.

Derek
0 Kudos
Message 5 of 5
(7,432 Views)