Add two radio buttons to the dialog box, radio1 and Radio2.
Question 1: How do I select radio1 or Radio2 by default? How do I know which one has been selected?
The key is to select the option. You only need to place the "default" option in oninitdialog. You can choose one of the three methods,
First:
(Cbutton *) getdlgitem (idc_radio1)-> setcheck (true); // select
(Cbutton *) getdlgitem (idc_radio1)-> setcheck (false); // do not select
(Cbutton *) getdlgitem (idc_radio1)-> getcheck (); if 1 is returned, the value 0 indicates that the value is not selected.
Second:
Associate a congtrol variable (subclass), CTRL + W (enable classwizard), and enable member variables and variables? Why is there no idc_radio1 ID? It turns out that there are no groups. Because radio buttons are usually used in groups, they are mutually exclusive in a group. Cancel. Go back to the Resource Panel of the dialog box and right-click radio1 to view the properties and select the group. Then, radio1 and Radio2 are in the same group (How do you know they are in the same group? Later ). Now, you can add the congtrol variable m_ctrlradio1 to radio1. As follows:
M_ctrlradio1.setcheck (true );
You can also use getcheck () to obtain the status.
Third:
Associate an int-type variable (also need to be grouped first) m_nradio1. Open the constructor in the dialog box and you will find:
M_nradio1 =-1; m_nradio1 do not assign a value-1, indicating that none of them are selected. If you change-1 to 0, you will find that radio1 is selected by default. In this case, the value of m_nradio1 is 1, and the second is selected (the problem also exists here, which one is the first? Which is the second one ?). It is easy to get the status. After updatedata (true), you can determine the value of m_nradio1.
Question 2: How to use multiple groups?
Multiple groups are used in the same way as a group. You only need to know which group is used. Add radio3 and radio4 to the dialog box. It's easy to arrange the order of these radio buttons first (this must be done. For example, if your group of controls have radio1, Radio2, and radio3, set their tab order to 1, 2, 3. Set the group attribute of radio1 to true. In this way, when radio1 is selected, the associated variable is 0. When Radio2 is selected, the associated variable is 1, and so on) is to sort their tab order. On the Resource Panel of the dialog box, press Ctrl + D and click one by one in the desired order. Assume that radio1, Radio2, radio3,
Radio4 is 1, 2, 3, and 4 respectively. Radio1 and radio3 both select the group attribute. Therefore, 1 and 2 are a group, and 3 and 4 are another group, because the grouping principle is to select the group attribute until the next one is met. CTRL + D again to make radio1, Radio2, radio3, and radio4 respectively 1, 3, 2, and 4. Then radio1 and radio3 are a group. If m_nradio1 = 1, in this case, radio3 is selected instead of Radio2. Use them separately after grouping.
Well, it's very easy to add mouse-click events for them.
1. Group Single-choice buttons:
Set attributes for the first single-choice button in each group: group, tabstop, and auto. Set attributes for other buttons: tabstop and auto. For example:
Radio1, Radio2, and radio3 are a group, and radio4 and radio5 are a group.
Set Radio1 attributes: Group, Tabstop, Auto
Set the Radio2 attribute: Tabstop, Auto
Set the Radio3 attribute: Tabstop, Auto
Set Radio4 attributes: Group, Tabstop, and Auto
Set the Radio5 attribute: Tabstop, Auto
2. Use ClassWizard to define variables for single-choice controls. Each group can only define one variable. For example, m_Radio1 and m_Radio4.
3. Use ClassWizard to generate the click message function of each radio button and add the following content:
Void CWEditView: OnRadio1 ()
{
M_Radio1 = 0; // The first radio button is selected.
}
Void CWEditView: OnRadio2 ()
{
M_Radio1 = 1; // The second radio button is selected.
}
Void CWEditView: OnRadio3 ()
{
M_Radio1 = 2; // The third radio button is selected.
}
Void CWEditView: OnRadio4 ()
{
M_Radio4 = 0; // The fourth radio button is selected.
}
Void CWEditView: OnRadio5 ()
{
M_Radio4 = 1; // The fifth radio button is selected.
}
4. Set the default button:
When defining a control variable, ClassWizard sets the initial value of the variable to-1 in the constructor. You only need to change it to another value.
For example:
// {AFX_DATA_INIT (CUnitBlockTypeFlankPublicAdd)
M_Radio1 = 0; // The first radio button is selected at the beginning.
M_Radio4 = 0; // The fourth radio button is selected in the initial stage.
//} AFX_DATA_INIT