CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2013
    Posts
    32

    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!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Radio Button help needed

    Quote Originally Posted by clow View Post

    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.

  3. #3
    Join Date
    Jun 2013
    Posts
    32

    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!

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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())

  5. #5
    Join Date
    Jun 2013
    Posts
    32

    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?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Radio Button help needed

    Yeah. You shouldn't be calling SetCheck yourself. Radio buttons do that for you.

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    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
  •  





Click Here to Expand Forum to Full Width

Featured