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, that 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, and Radio4 are respectively 1, 2, 3, and 4. 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
From abcjennifer's column