Programming with Text Button Controls
- Updated2023-02-21
- 2 minute(s) read
Programming with Text Button Controls
This topic describes how to complete the following tasks programmatically.
- Creating a text button control
- Setting and obtaining the state of a button
- Setting and obtaining the string associated with a text button control
- Setting the on/off color and text styles
Creating a Text Button Control
Use the NewCtrl function to create a text button control.
int textButton;
textButton = NewCtrl (panelHandle, CTRL_SQUARE_TEXT_BUTTON_LS, "Text Button Control", 25, 45);
Setting and Obtaining the State of a Button
Call SetCtrlVal to set the state of a text button.
int val;
GetCtrlVal (panelHandle, PANEL_BINARYSWITCH, &val);
SetCtrlVal (panelHandle, textButtonCtrl, val);
Call GetCtrlVal to get the state of a text button.
int state;
GetCtrlVal (panelHandle, textButtonCtrl, &state);
SetCtrlVal (panelHandle, PANEL_NUMERIC_2, state);
Setting and Obtaining the String Associated with a Text Button Control
By default, the text button text is OFF and ON. You can specify different values with attributes.
SetCtrlAttribute (panelHandle, textButtonCtrl, ATTR_ON_TEXT, "Running");
SetCtrlAttribute (panelHandle, textButtonCtrl, ATTR_OFF_TEXT, "Stopped");
Use the same attributes to obtain the value of the text.
int onTextLen, offTextLen;
char *onText = NULL, char *offText = NULL;
GetCtrlAttribute (panelHandle, textButtonCtrl, ATTR_ON_TEXT_LENGTH, &onTextLen);
The ATTR_ON_TEXT and ATTR_OFF_TEXT attributes append a NUL byte to the end of the text string, so you must make the buffer 1 byte larger than the value returned by ATTR_ON_TEXT_LENGTH and ATTR_OFF_TEXT_LENGTH.
onText = malloc (sizeof(char) * (onTextLen + 1));
GetCtrlAttribute (panelHandle, textButtonCtrl, ATTR_ON_TEXT, onText);
GetCtrlAttribute (panelHandle, textButtonCtrl, ATTR_OFF_TEXT_LENGTH, &offTextLen);
offText = malloc (sizeof(char) * (offTextLen + 1));
GetCtrlAttribute (panelHandle, textButtonCtrl, ATTR_OFF_TEXT, offText);
Setting the On/Off Color and Text Styles
SetCtrlAttribute (panelHandle, textButtonCtrl, ATTR_TEXT_BOLD, 1);
SetCtrlAttribute (panelHandle, textButtonCtrl, ATTR_ON_COLOR, VAL_GREEN);
SetCtrlAttribute (panelHandle, textButtonCtrl, ATTR_OFF_COLOR, VAL_RED);
SetCtrlAttribute (panelHandle, textButtonCtrl, ATTR_TEXT_POINT_SIZE, 25);
SetCtrlAttribute (panelHandle, textButtonCtrl, ATTR_HEIGHT, 40);