VC6 in MFC in the Radio box and check box control is not simple, it does not have a vs in C # that can be judged by a package after the function, the Radio box/check box is not selected AH? or control the selected state of the Radio box/check box. This thing is more complicated and is controlled by the corresponding value.
I. BASIC OBJECTIVES
Here is a simple example to illustrate the VC6 in MFC in the Radio box and check box control, click the "OK" button to determine the dialog box of all the Radio box and check box selection status. Radio1-radio3 into a group, the group can only have a team member is selected, Radio4-radio5 also a group, select Radio4 can make the check box CHECK1-CHECK3 Select All, choose Radio5 to implement the check box anti-selection function. The group box control is also introduced for aesthetics.
Second, the production process
1, create a new one without a dialog-based, static DLL MFC, how to create a new can see me before the "MFC" Student information management, the implementation of the list control node additions and deletions (click the Open link), and then in the dialog box, drag into the Group box, radio box and check box. Group box There is nothing to say, static text is just as true, only the display function, do not set any function. You just need to change the caption.
2. Then the only thing to note is the Radio box style tool, in the style of the radio box, if you select a radio box for the group, then until the next one is selected as the "Group" Radio box, all belong to this "group", the group's radio box can only have a suspended spring, for example, it is clear. For example, there are five radio1-5 in the box, if only the Radio1 set as a group, then radio1-radio5 belong to this group, RADIO1-RADIO5 can only have one selected. If you set Radio1 and Radio4 as groups, like the same, then Radio1-radio3 can only have one selected, Radio4-radio5 can only have one selected. Again, if the Radio1,radio3,radio5 is selected as a group, then Radio1-radio2, Radio3-radio4, Radio5 can only select one respectively. In contrast to C #, setting a radio box in the same group to the same groudid is different.
3. Then set the member variables on the Radio box/checkbox in the main dialog box so that they can be programmed, as well as ctrl+w or otherwise create a new Class Wizard for this dialog box, except that this is set in the tab of the member variable.
4, the type of the general member variable by default, basically there is no room for your choice, MFC is defined by the Boolean value is and not to determine whether this check box is selected.
5, pay attention to observation, only the radio box selected as "group" has the member variable settings, in fact, the "group" radio box already contains all the members thereafter. It is judged by the int value that the radio box in the group is selected, 1 is not selected for all the radio boxes in the group, 0 is the first in the group, and 1 is the second within the group ... And so on
6. After setting the completion member variable for all the radio boxes/checkboxes, as shown in:
7, click "OK" can start the function of the writing, you can find the member variables just set in the ClassView, and can be a single box/check box of the initial condition, such as M_check1 to True, then this dialog box just started, check box Check1 is selected, and m_radio1_3 you change it to 0-2, then the corresponding Radio1-3 is initially selected. Don't ask me to change the value of more than 3, then only the program error, Win32 crashes.
8, after, you can also like the button, in the control interface, double-click any one of the Radio box/check box, add this radio box/check box when the message map function is selected. For example, when we add a check box to the Radio4 box, the Select All function is written as follows:
void Ccheckbox_radiodlg::onradio4 () {//Note here do not write M_check1,m_check2 ... The form for (int i=idc_check1;i<=idc_check3;i++) {((cbutton*) GetDlgItem (i))->setcheck (1);}}
((cbutton*) GetDlgItem (i))->setcheck (1); ability to set the value of a check box while Idc_check1-idc_check3 as the control's ID, but essentially an int, can be traversed as shown above.
9, and select Radio5, the real inverse of the function, so Radio5 functions like the following function:
Anti-select void Ccheckbox_radiodlg::onradio5 () {//Todo:add your control notification handler code herefor (int I=IDC_CHECK1;I&L t;=idc_check3;i++) {//Through ((cbutton*) GetDlgItem (i))->getcheck () can also get the value of the check box//The value of the radio box is no problem either//But this function value is a record, You have a choice. This Radio box/check box if (((cbutton*) GetDlgItem (i))->getcheck ()) {((cbutton*) GetDlgItem (i))->setcheck (0);} else{((cbutton*) GetDlgItem (i)->setcheck (1);}} }
10, as to click on the "OK" button and determine the control of the dialog box is selected, so OnOK should write this, using the conditional structure to select the box/check box to execute the corresponding statement.
void Ccheckbox_radiodlg::onok () {//Todo:add extra Validation Here//cdialog::onok ();//This sentence must exist, meaning to re-traverse all the controlled variable values, Read the user-selected values in UpdateData (); CString Str;switch (m_radio1_3) {case-1:str= "Radio box 1_3, you have nothing to choose!" "; break;case 0:str=" Radio box 1_3, you choose you Radio1 "; break;case 1:str=" Radio box 1_3, you select you Radio2 "; break;case 2:str=" Radio box 1_3, you choose you Radio3 "; break;} AfxMessageBox (str); switch (m_radio4_5) {case-1:str= "Radio box 4_5, you have no choice! "; break;case 0:str=" Radio box 4_5, you choose you Radio4 "; break;case 1:str=" Radio box 4_5, you choose you Radio5 "; AfxMessageBox (str); str= "checked:", if (m_check1) str+= "" check box 1 "", if (m_check2) str+= "" check box 2 "", if (M_CHECK3) str+= "" check box 3 ""; if ( !M_CHECK1&&!M_CHECK2&&!M_CHECK3) str= "You did not select any check box! "; AfxMessageBox (str);}
MFC group box, radio box control and check box control