The usage of radio button in MFC _c language

Source: Internet
Author: User

In this paper, the usage of radio button in MFC is elaborated through a solution for two common problems. Helps readers deepen understanding of MFC's operating mechanisms and the usage of radio button controls.

First, add 2 radio button to the dialog box, respectively, Radio1 and Radio2.

Question 1: How do I get Radio1 or Radio2 to be selected by default? How do you know which one has been chosen?

The key is to select,"Default" as long as the OnInitDialog () can be. Here are three ways to choose:

First type:

((CButton *) GetDlgItem (idc_radio1))->setcheck (TRUE);//Selected
((CButton *) GetDlgItem (idc_radio1))->setcheck (FALSE);//Not selected
((CButton *) GetDlgItem (idc_radio1))->getcheck ();//return 1 for election, 0 for no election.

The second type:

Associate a Congtrol type variable (subclass), good ctrl+w (that is, open ClassWizard), point Open Member Variables, Yi? Why didn't idc_radio1 this ID? The original is not grouped. Because the radio button is usually used in groups, it is mutually exclusive in a group. Cancel, back to the dialog resource panel, right-click Radio1 View attributes to select group, then Radio1 and Radio2 are a group (how do you know they are a group?) Said later). At this point, you can add a Congtrol variable m_ctrlradio1 for Radio1. As follows:

M_ctrlradio1.setcheck (TRUE);

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

The third type:

To associate an int type variable (which also needs to be grouped first) M_nradio1, open the dialog box constructor, and you will find that there are:

M_nradio1 =-1;

M_nradio1-1 means that none of the options are selected. If you change to 1 to 0, you will find that Radio1 is selected by default, and so on, the M_nradio1 value is 1 is the second is selected (there is also a problem, which is the first one?) Which is the second one? )。 Getting the state is simple,updatedata (TRUE) to determine the value of the M_nradio1 .

Question 2: How do I use multiple groups?

Multiple groups and a group are the same use, just figure out which one is the right group. Add Radio3 and Radio4 to the dialog box again. Quite simply, first order the radio button, which is to arrange their tab orders. Ctrl+d on the dialog resource panel and click on the mouse in your own ideal order. It may be assumed that Radio1, Radio2, Radio3, Radio4 are 1, 2, 3, 4 respectively. Radio1 and Radio3 are all selected on the group attribute, then 1, 2 is a set, 3, 4 is another group, because the principle of grouping is to select this one of the group attribute until it encounters the next selected group attribute. You may as well ctrl+d, so that Radio1, Radio2, Radio3, Radio4 are 1, 3, 2, 4, then Radio1 and Radio3 is a group, if m_nradio1=1, at this time Radio3 is chosen instead of Radio2 selected. Divide the group and use them separately.
Well, maybe you should also add a mouse click event for them, very simple.

First, the radio buttons are grouped:

The first radio button in each group sets properties: Group,tabstop,auto; The remaining buttons set properties Tabstop,auto. Such as:
Radio1, Radio2, Radio3 as a group, Radio4, Radio5 as a group
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

Two, use ClassWizard for the radio control to define variables, each group can only define one. such as: M_radio1, M_radio4.

Third, use ClassWizard to generate each radio button click Message function, and add 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
}

Four, set the default button:

When you define a control variable, ClassWizard sets the variable initial value to 1 in the constructor, just change it to something else.
such as:

//{{afx_data_init (cunitblocktypeflankpublicadd) m_radio1 = 0;  Initially, the first radio button is selected M_radio4 = 0; The fourth radio button is selected at the beginning//}}afx_data_init 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.