Programming with Controls
This topic describes how to complete the following tasks programmatically.
- Adding callbacks
- Adding tooltips
- Assigning default values to controls
- Creating a new control
- Discarding a control
- Duplicating a control
- Obtaining the active control
- Obtaining the dimensions of the rectangle surrounding the control and its label
- Printing controls
- Referring to controls
- Setting and obtaining a value for a control
- Setting and obtaining an attribute of a control
- Specifying the active control
Creating a New Control
NewCtrl creates a new control during program execution. NewCtrl returns an ID you use to reference the control in subsequent operations. Use the first parameter of NewCtrl to specify the panel on which the control is to appear. The second parameter specifies the control style. You also specify the label and position of the control through parameters.
int numCtrl;
numCtrl = NewCtrl (panelHandle, CTRL_NUMERIC_LS, "Numeric Control", 30, 30);
You can use numCtrl to reference the control in subsequent functions.
Discarding a Control
DiscardCtrl removes a control from memory. If its panel is visible, the control is removed from the screen. LabWindows/CVI does not allow you to call DiscardCtrl from the callback function for the control, except in response to a commit event. A call to DiscardCtrl from any other type of event might cause unpredictable behavior.
DiscardCtrl (panelHandle, PANEL_NUMERIC);
Duplicating a Control
DuplicateCtrl creates a new control that is a duplicate of an existing control. DuplicateCtrl returns an ID that you use to reference the control in subsequent operations.
int duplicateNum;
duplicateNum = DuplicateCtrl (panelHandle, CTRL_NUMERIC_LS, childPanel, "Duplicate Control", VAL_KEEP_SAME_POSITION, VAL_KEEP_SAME_POSITION);
Adding Callbacks
Call InstallCtrlCallback to assign a callback function to execute when an event is generated on the control.
InstallCtrlCallback (panelHandle, PANEL_NUMERIC, MyCallback, 0);
When an event occurs on PANEL_NUMERIC, MyCallback is called.
/* In this example, MyCallback responds to right-click events. However, callback functions can respond to many events. */
int CVICALLBACK MyCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
if (event == EVENT_RIGHT_CLICK)
{
MessagePopup ("Message", "Right-click on control");
}
return 0;
}
Adding Tooltips
Call the SetCtrlAttribute function to display and configure control tooltips.
SetCtrlAttribute (panelHandle, PANEL_NUMERIC, ATTR_DISABLE_CTRL_TOOLTIP, 0);
SetCtrlAttribute (panelHandle, PANEL_NUMERIC, ATTR_TOOLTIP_TEXT, "A numeric control displays numeric values");
Assigning Default Values to Controls
DefaultCtrl restores a control to its default value. If the control is visible, the program updates it to reflect its default value.
SetCtrlAttribute (panelHandle, PANEL_NUMERIC, ATTR_DFLT_VALUE, 5.00);
DefaultCtrl (panelHandle, PANEL_NUMERIC);
Specifying the Active Control
SetActiveCtrl establishes the control that receives keyboard events when its panel is the active panel.
SetActiveCtrl (panelHandle, PANEL_NUMERIC);
Obtaining the Active Control
GetActiveCtrl obtains the control that receives keyboard events when its panel is the active panel.
int activeCtrl;
activeCtrl = GetActiveCtrl (panelHandle);
Obtaining the Dimensions of the Rectangle Surrounding the Control and its Label
GetCtrlBoundingRect obtains the top, left, height, and width of the rectangle bounding the control and its label.
int top, left, height, width;
GetCtrlBoundingRect (panelHandle, PANEL_NUMERIC, &top, &left, &height, &width);
The coordinates of the bounding rectangle are useful for spacing controls on the user interface.
Printing Controls
Use PrintCtrl to print the selected control.
PrintCtrl (panelHandle, PANEL_NUMERIC, "printer file", 1, 1);
Referring to Controls
When you use LoadPanel to load a into memory, it also loads the controls on that panel. To refer to a particular control in a call to a User Interface Library function, you can use the defined constant assigned to the control in the User Interface Editor or the ID returned by one of the functions that creates a control.
// Refer to a control by constant.
SetActiveCtrl (panelHandle, PANEL_TREE);
// Refer to a control by ID.
SetActiveCtrl (panelHandle, treeCtrl);
Setting and Obtaining a Value for Control
When you specify a value for a control, you must make sure that the value you specify matches the data type of the control. SetCtrlVal sets a control to a particular value.
SetCtrlVal (panelHandle, PANEL_NUMERIC, 5.00);
GetCtrlVal obtains the current value of a control.
GetCtrlVal (panelHandle, PANEL_NUMERIC, &controlVal);
Setting and Obtaining an Attribute of a Control
SetCtrlAttribute sets an attribute of a control. When changing control attributes that affect the font of a control, set the ATTR_TEXT_FONT attribute first.
GetCtrlAttribute obtains the value of the attribute for the control.
SetCtrlAttribute (panelHandle, PANEL_NUMERIC, ATTR_TEXT_FONT, VAL_MESSAGE_BOX_FONT);
SetCtrlAttribute (panelHandle, PANEL_LED, ATTR_ON_COLOR, VAL_BLUE);
GetCtrlAttribute (panelHandle, PANEL_LED, ATTR_ON_COLOR, &ledcolor);
You can use SetCtrlAttribute and GetCtrlAttribute with all LabWindows/CVI user interface controls.