|
|
|
Check boxes and radio buttonsYou know what check boxes and radio buttons are. Let's see how to use them in MFC applications. Check Boxes Check boxes are used to ask the user whether he needs some option or not. User can check the check box to say that he needs the option, or uncheck the check box to mention that he do not need the option. Using check boxes in MFC applications is also very easy. You can simply drag a check box and change its caption to something meaningful to indicate what the user can select or deselect by it. Similar to the way you added a variable to an edit control in Getting started with MFC, you have to add a variable to the check box by right clicking it and select Add Variable. This time also, you should select "value" for the category of the variable. The type of variable should be bool. You can give any meaningful name to the variable. The following example shows how to use a check box in a MFC application. Note that the application doesn't do anything meaningful other than showing you how to use a check box. When you press a button, it will just give a message telling whether you have checked the checkbox or not, at the time you press the button. void CchkboxDlg::OnBnClickedButton1() Here, the variable you added to the check box is "close". We have to first update the content of this variable to the state of the checkbox, so we call UpdateData(TRUE). Then, if the check box is checked, variable "close" will have the value true. So, if it is true, we give a message telling user has checked it and close the dialog by calling OnOK(). If "close" is not true, we just give the message that user has not checked the checkbox. Download the source code here. Radio buttons Radio buttons look similar to check boxes but the difference is several radio buttons belong to one group and only one radio button in a group can be in the checked state. So, they are used to ask the user what is his selection from various things, from where he can select only one thing. You know we assign variable to controls such as text boxes, check boxes etc to get the value of the control, and the type of the variable used for each control depends on the functionality of the control. So, what is the suitable type for a radio button? It is int. Say we have 3 radio buttons belonging to the same group. We assign one int variable to the whole group. If the user selects the first radio button, the value of our variable will be 0, if he selects the second radio, the value will be 1 and so on. Lets take a small example. Think you have 2 groups of radio buttons, first having 2 radio buttons and the second having 3 radio buttons. When you drag radio buttons to your dialog, you have to specify which radio button belong to which group in some way. To do that, follow the steps given below.
Now, the first 2 radio buttons you added act as one group, and next 3 radio buttons act as another group. Remember, the order you added the radio buttons is important. If you do not do this correctly, the buttons you added may not behave as you wanted, and to correct the behavior, you have to edit the resource file of the project manually using a text editor. That is a bit difficult task. The following simple application will help you understand how to use radio buttons.
Here, you can see 2 groups of radio buttons (Just because the radio buttons are placed close, you can't say they belong to the same group. You have to set the "group" property correctly as mentioned above to do that). Here is the function which is called when you click OK. void CradioDlg::OnBnClickedButton1() Here, the variable assigned for first group is "sex" and for second group, the variable is "height". If the user selected male from first group and above 180 in the second group, variable "sex" will contain 0 and variable height will contain 2. So, the message he will get is "you are male above 180 cm". Download the source code of sample program here. |