Detailed explanation of the use of Radio Button in MFC

  • 2020-04-02 02:31:54
  • OfStack

This article explains the use of Radio Button in MFC in detail through solutions to two common problems. It helps the reader to have a better understanding of the MFC operation mechanism and the use of the Radio Button control.

First, add two radio buttons, Radio1 and Radio2, to the dialog box.

Question 1: how do I make Radio1 or Radio2 selected by default? How do you know which one is chosen?

The key is to select, and the "default" is simply placed on the OnInitDialog(). There are three ways to select it:

The first:


((CButton *)GetDlgItem(IDC_RADIO1))->SetCheck(TRUE);//selected
((CButton *)GetDlgItem(IDC_RADIO1))->SetCheck(FALSE);// Don't selected
((CButton *)GetDlgItem(IDC_RADIO1))->GetCheck();// return 1 said selected . 0 Said no selected

The second:

Concatenate a congtrol variable (subclassed), ok CTRL +W, click Member Variables, huh? Why is there no ID IDC_RADIO1? There was no grouping. Because radio buttons are usually used in groups, they are mutually exclusive within a group. Cancel, go back to the dialog box resources panel, right-click on Radio1 view properties and select Group. Then, Radio1 and Radio2 are a Group. Later on). At this point, you can add a congtrol variable m_ctrlRadio1 to Radio1. As follows:


m_ctrlRadio1.SetCheck(TRUE);

You can also use GetCheck() to get the status.

The third:

Associate m_nRadio1 with an int (again grouped), open the dialog constructor and you will find:


m_nRadio1 = -1;

M_nRadio1 does not assign -1 to indicate that neither is selected. If you change negative 1 to 0, you will see that Radio1 is selected by default, and so on. If m_nRadio1 is 1, the second is selected. Which is the second? . It's easy to get the status, just determine the value of m_nRadio1 after UpdateData(TRUE).

Question 2: how to use multiple groups?

Multiple groups are the same as one group, just make sure you know which is which. Add Radio3 and Radio4 to the dialog box. It's easy to put these Radio buttons in ORDER, which is their TAB ORDER. Ctrl+D on the dialog resource panel, and then click the mouse one by one in your desired order. Let's say that Radio1, Radio2, Radio3, and Radio4 are 1, 2, 3, and 4, respectively. Radio1 and Radio3 both select the Group attribute, so 1, 2 is one Group, and 3, 4 is another Group, because the principle of grouping is to select the Group attribute until the next Group attribute is selected. You can also Ctrl+D, let Radio1, Radio2, Radio3, Radio4 are 1, 3, 2, 4, so Radio1 and Radio3 is a group, if m_nRadio1=1, this is Radio3 is selected instead of Radio2 is selected. Use them separately when you have a group.
Well, you might want to add mouse clicks to them, too, very simply.

1. Group radio buttons:

The first radio button of each Group sets properties: Group, Tabstop, Auto; The rest of the buttons set the properties Tabstop, Auto. Such as:
Radio1, Radio2, Radio3 in groups, Radio4, Radio5 in groups
Set Radio1 properties: Group, Tabstop, Auto
Set Radio2 properties: Tabstop, Auto
Set Radio3 properties: Tabstop, Auto
Set Radio4 properties: Group, Tabstop, Auto
Set Radio5 properties: Tabstop, Auto

Use the ClassWizard to define variables for the radio control, one for each group. For example: m_Radio1, m_Radio4.

Use ClassWizard to generate the click message function of each radio button and add the 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
}

Iv. Setting default button:

When defining a control variable, the ClassWizard sets the initial value of the variable to -1 in the constructor, simply changing it to something else.
Such as:


//{{AFX_DATA_INIT(CUnitBlockTypeFlankPublicAdd)
m_Radio1 = 0;  //Initially the first radio button is selected
m_Radio4 = 0;  //Initially the fourth radio button is selected
//}}AFX_DATA_INIT

Related articles: