Admin Production ni-theme
Current Publication

Programming with Binary Switch Controls

LabWindows/CVI

Programming with Binary Switch Controls

This topic describes how to complete the following tasks programmatically.

Creating a Binary Switch Control

Use NewCtrl to create a binary switch control.

int binarySwitch;
binarySwitch = NewCtrl (panelHandle, CTRL_TOGGLE_VSWITCH, "Binary Switch", 20, 20);

Setting and Obtaining the On and Off Text

Use SetCtrlAttribute to set the text of a binary switch control.

SetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_ON_TEXT, "Yes");
SetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_OFF_TEXT, "No");

Use GetCtrlAttribute to get the value of a binary switch text. You must make the buffer 1 byte larger than the value returned by GetCtrlAttribute.

int onTextLength, offTextLength;
char *onText = 0, *offText = 0;
GetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_ON_TEXT_LENGTH, &onTextLength);
GetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_OFF_TEXT_LENGTH, &offTextLength);
onText = malloc ((onTextLength + 1) * sizeof (char));
offText = malloc ((offTextLength + 1) * sizeof (char));
GetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_ON_TEXT, onText);
GetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_OFF_TEXT, offText);

Setting and Obtaining the On and Off Values

Use SetCtrlAttribute and GetCtrlAttribute with the ATTR_ON_VALUE and ATTR_OFF_VALUE attributes to set and get the values corresponding to the on and off states of the binary switch.

SetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_ON_VALUE, 1);
SetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_OFF_VALUE, 0);

int onValue, offValue;
GetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_ON_VALUE, &onValue);
GetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_OFF_VALUE, &offValue);

Setting and Obtaining the State of a Binary Switch

Use SetCtrlVal to set the state of a binary switch by passing the value that corresponds to the desired state.

SetCtrlVal (panelHandle, PANEL_BINARYSWITCH, 1);

Use GetCtrlVal to obtain the value corresponding to the current state of a binary switch.

int currentVal;
GetCtrlVal (panelHandle, PANEL_BINARYSWITCH, &currentVal);

Associating the Data Type of the Binary Switch with Values

The data type for a binary switch control determines the type of data you can use for the on and off value of binary switch controls. You must be careful to match the value you pass to SetCtrlVal, as well as the value you use to set ATTR_ON_VALUE and ATTR_OFF_VALUE, with the data type of the control. The pointer you pass to GetCtrlAttribute and to GetCtrlVal also must be compatible with the current data type of the control.

To set the data type, use the ATTR_DATA_TYPE attribute.

SetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_DATA_TYPE, VAL_STRING);

If the data type of the binary switch is VAL_STRING, then use GetCtrlAttribute with the ATTR_ON_VALUE_LENGTH and ATTR_OFF_VALUE_LENGTH attributes to determine the size of the buffer needed to get the value strings. Keep in mind that GetCtrlAttribute and GetCtrlVal append a NULL byte to the end of the text string, so you must make the buffer 1 byte larger than the value obtained using ATTR_ON_VALUE_LENGTH or ATTR_OFF_VALUE_LENGTH.

int onValLength, offValLength;
char *onVal = 0, *offVal = 0;
GetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_ON_VALUE_LENGTH, &onValLength);
GetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_OFF_VALUE_LENGTH,
&offValLength);
onVal = malloc ((onValLength + 1) * sizeof (char));
offVal = malloc ((offValLength + 1) * sizeof (char));
GetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_ON_VALUE, onVal);
GetCtrlAttribute (panelHandle, PANEL_BINARYSWITCH, ATTR_OFF_VALUE, offVal);

Related Topics

Programming with Controls

Binary Switch Control Functions