Programming with String Controls
This topic describes how to complete the following tasks programmatically.
ATTR_STRING_TEXT_LENGTH attribute to determine the size of the- Binding to a DataSocket source
- Creating a string control
- Getting the number of bytes of text
- Masking characters
- Setting a limit on characters entered
- Setting and obtaining a string value
- Setting text styles
- Setting the string control to be not editable
Creating a String Control
Use NewCtrl to create a string control.
int stringCtrl;
stringCtrl = NewCtrl (panelHandle, CTRL_STRING_LS, "String Control", 30, 30);
Setting and Obtaining a String Value
Call SetCtrlVal to specify the text that displays in the string control.
SetCtrlVal (panelHandle, stringCtrl, "Enter your name");
GetCtrlVal obtains the text in the string control. GetCtrlVal appends a NUL byte to the end of the text string, so you must make the buffer 1 byte larger than the value returned by GetCtrlAttribute.
static int stringLength;
static char *stringVal = 0;
GetCtrlAttribute (panelHandle, stringCtrl, ATTR_STRING_TEXT_LENGTH, &stringLength);
stringVal = malloc ((stringLength + 1) * sizeof (char));
GetCtrlVal (panelHandle, stringCtrl, stringVal);
Binding to a DataSocket Source
(Linux) Data binding is not supported.
Controls can have only one DataSocket connection. You must specify whether the access mode is READ mode or WRITE mode:
- READ mode—The control gets data from the DataSocket source. In READ mode, the value of the string control is updated automatically when the value changes at the source.
- WRITE mode—The control transfers data to the DataSocket source. In WRITE mode, the value at the DataSocket source is updated automatically when the value of the string control changes.
You must enable the DataSocket option in the Edit Installer dialog box Drivers & ComponentsDrivers & Components tab if your applications use data binding.
Binding to a DataSocket Source in the User Interface Editor
Complete the following steps to bind a network variable to a string control.
- In the Edit String dialog box, click the DataSocket Binding button.
- Click the browse button to the right of the Server text box in the DataSocket Binding dialog box.
- Select the network variable you want to bind and click the OK button.
- Select whether you want to read or write data in the Connection Type option.
- Click the OK button.
Binding to a DataSocket Source Programmatically
Use DSBindCtrl to bind a string control to a DataSocket source. The DataSocket source must hold text data.
HRESULT dsError;
static DSHandle dsHandle;
DSBindCtrl (panelHandle, stringCtrl, "opc://localhost/National Instruments.OPCDemo/MotorRPMSensor", VAL_DS_READ, &dsHandle, &dsError);
Use DSUnbind to disconnect the string control from the DataSocket source.
DSUnbind (panelHandle, stringCtrl, &dsError);
Setting Text Styles
SetCtrlAttribute (panelHandle, PANEL_TEXTSTRING, ATTR_TEXT_BGCOLOR, VAL_DK_MAGENTA);
SetCtrlAttribute (panelHandle, PANEL_TEXTSTRING, ATTR_TEXT_COLOR, VAL_WHITE);
SetCtrlAttribute (panelHandle, PANEL_TEXTSTRING, ATTR_TEXT_POINT_SIZE, 18);
Setting a Limit on Characters Entered
If you want to limit how many characters are entered into a string control, use the ATTR_MAX_ENTRY_CHARS attribute. LabWindows/CVI does not allow entry past the limit.
SetCtrlAttribute (panelHandle, stringCtrl, ATTR_MAX_ENTRY_CHARS, 20);
Setting the String Control to be Not Editable
Use the ATTR_NO_EDIT_TEXT attribute to keep the string control from being modified. This attribute does not change the string control to look like it is in indicator mode.
SetCtrlAttribute (panelHandle, stringCtrl, ATTR_NO_EDIT_TEXT, 1);
Getting the Number of Bytes of Text
int totalLen, selLen;
/* Get the total number of bytes in the string control */
GetCtrlAttribute (panelHandle, PANEL_TEXTSTRING, ATTR_STRING_TEXT_LENGTH, &totalLen);
/* Get the number of bytes of selected text */
GetCtrlAttribute (panelHandle, PANEL_TEXTSTRING, ATTR_TEXT_SELECTION_LENGTH, &selLen);
SetCtrlVal (panelHandle, PANEL_TOTAL, totalLen);
SetCtrlVal (panelHandle, PANEL_SELECTED, selLen);
Masking Characters
If you do not want to display the actual characters being entered into the string control, you can use character masking.
SetCtrlAttribute (panelHandle, stringCtrl, ATTR_ENABLE_CHARACTER_MASKING, 1);
SetCtrlAttribute (panelHandle, stringCtrl, ATTR_MASK_CHARACTER, '#');