-
January 27th, 2014, 08:52 AM
#1
Radio Button help needed
Hello,
I have a group of 3 radio buttons with IDs:
Code:
IDC_RADIO_ONE
IDC_RADIO_TWO
IDC_RADIO_THREE
Only IDC_RADIO_ONE has the Group property set to True. The rest are false. I have an int declared as:
Code:
int iRadioSelection_;
which is initialized to 0 in the class constructor.
Next I have:
Code:
void RadioDialog::DoDataExchange(CDataExchange* pDX)
{
DDX_Radio(pDX, IDC_RADIO_ONE, iRadioSelection_);
...
}
BEGIN_MESSAGE_MAP(RadioDialog, CDialog)
ON_BN_CLICKED(IDC_RADIO_ONE, &ConfigureDialog::OnBnClickedRadioSelection)
ON_BN_CLICKED(IDC_RADIO_TWO, &ConfigureDialog::OnBnClickedRadioSelection)
ON_BN_CLICKED(IDC_RADIO_THREE, &ConfigureDialog::OnBnClickedRadioSelection)
...
END_MESSAGE_MAP()
void RadioDialog::OnBnClickedRadioSelection()
{
if(iRadioSelection_== 0)
{
((CButton*)GetDlgItem(IDC_RADIO_ONE))->SetCheck(true);
((CButton*)GetDlgItem(IDC_RADIO_TWO))->SetCheck(false);
((CButton*)GetDlgItem(IDC_RADIO_THREE))->SetCheck(false);
}
else if(iRadioSelection_== 1)
{
((CButton*)GetDlgItem(IDC_RADIO_ONE))->SetCheck(false);
((CButton*)GetDlgItem(IDC_RADIO_TWO))->SetCheck(true);
((CButton*)GetDlgItem(IDC_RADIO_THREE))->SetCheck(false);
}
else if(iRadioSelection_== 2)
{
((CButton*)GetDlgItem(IDC_RADIO_ONE))->SetCheck(false);
((CButton*)GetDlgItem(IDC_RADIO_TWO))->SetCheck(false);
((CButton*)GetDlgItem(IDC_RADIO_THREE))->SetCheck(true);
}
}
This does not seem to work. Am I missing something here?
Thanks!
-
January 27th, 2014, 09:06 AM
#2
Re: Radio Button help needed
Originally Posted by clow
This does not seem to work. Am I missing something here?
Thanks!
Yes. The event handlers don't update the member variable. You need to call UpdateData() or do it yourself manually.
-
January 27th, 2014, 09:12 AM
#3
Re: Radio Button help needed
So where do I call UpdateData() at exactly? I changed the code to:
Code:
void RadioDialog::OnBnClickedRadioSelection()
{
if(iRadioSelection_== 0)
{
((CButton*)GetDlgItem(IDC_RADIO_ONE))->SetCheck(true);
((CButton*)GetDlgItem(IDC_RADIO_TWO))->SetCheck(false);
((CButton*)GetDlgItem(IDC_RADIO_THREE))->SetCheck(false);
}
else if(iRadioSelection_== 1)
{
((CButton*)GetDlgItem(IDC_RADIO_ONE))->SetCheck(false);
((CButton*)GetDlgItem(IDC_RADIO_TWO))->SetCheck(true);
((CButton*)GetDlgItem(IDC_RADIO_THREE))->SetCheck(false);
}
else if(iRadioSelection_== 2)
{
((CButton*)GetDlgItem(IDC_RADIO_ONE))->SetCheck(false);
((CButton*)GetDlgItem(IDC_RADIO_TWO))->SetCheck(false);
((CButton*)GetDlgItem(IDC_RADIO_THREE))->SetCheck(true);
}
UpdateData();
}
And the button does not change when I click others in the group. Where should I add the UpdateData() call at?
Thanks!
-
January 27th, 2014, 09:14 AM
#4
Re: Radio Button help needed
First line in your event handler would work. Keep in mind it will update the values for any other controls you have too.
Another option would be just to check the state yourself
if(GetDlgItem(IDC_RADIO_ONE)->GetCheck())
-
January 27th, 2014, 09:23 AM
#5
Re: Radio Button help needed
Putting UpdateData() at the beginning of the handler had mixed results. First when I click IDC_RADIO_TWO then IDC_RADIO_THREE is selected. Also, IDC_RADIO_ONE stays selected also, until I move my mouse over it, then I guess it repaints itself.
Any other ideas what may be wrong?
-
January 27th, 2014, 09:53 AM
#6
Re: Radio Button help needed
Yeah. You shouldn't be calling SetCheck yourself. Radio buttons do that for you.
-
January 27th, 2014, 12:32 PM
#7
Re: Radio Button help needed
Are your radio buttons BS_RADIOBUTTON or BS_AUTORADIOBUTTON? I would suggest - BS_AUTORADIOBUTTON. Then, as GCDEF stated, you won't need SetCheck() calls.
I also suggest to set the next control (after you radio buttons in the tab order) to Group.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|