Usage of the VC radio button control (radio button)

Source: Internet
Author: User

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.
Sometimes the VC prompts that the getcheck () function is not available. You can use getcheckedradiobutton;

Cwnd: getcheckedradiobutton

Function: Obtain the ID of the selected single-choice button in a given group of single-choice IDs.

Function prototype: int getcheckedradiobutton (INT nidfirstbutton, int nidlastbutton );

Return Value: if a single region is selected, the ID of the selected single region is returned. If none of the groups are selected, 0 is returned.

Note: In MFC, the control IDs of these buttons must be continuously increased!

Parameters:

ID of the first integer in the nidfirstbutton single Response Group

ID of the last integer value in the nidlastbutton single Response Group

switch(GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO4))

                             {                                     case  IDC_RADIO1:                                              MessageBox(_T( "100" ));                                              break ;                                     case  IDC_RADIO2:                                              MessageBox(_T( "90" ));                                              break ;                                     case  IDC_RADIO3:                                              MessageBox(_T( "80" ));                                              break ;                                     case  IDC_RADIO4:                                              MessageBox(_T( "70" ));                                              break ;                              }


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, 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

 

 

Reprint: http://club.topsage.com/thread-670221-1-1.html,

Modified

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.