0

I have a radio button that is a CButton in a CDialog. When the user clicks the radio button, the function OnClickedRadioButton is called.

Inside OnClickedRadioButton I toggle the button by calling this function:

void toggleButton(CButton& theButton)
{
    switch(theButton.GetCheck())
    {
        case BST_UNCHECKED:
        {
            theButton.SetCheck(BST_CHECKED);
            break;
        }
        case BST_CHECKED:
        {
            theButton.SetCheck(BST_UNCHECKED);
            break;
        }
        default:
        {
            theButton.SetCheck(BST_UNCHECKED);
        }
    }
}

When I compile & run the program: (i) if the radio button is checked on, I can click it to clear it. (ii) if the radio button is unchecked, I click it and nothing happens. But if I click on a different program (i.e. visual studio) and then click back on the CDialog, the radio button checks on.

I've looked & tried functions Cwnd::UpdateDialogControls and Cwnd::UpdateData, but I was not able to get these to solve my problem.

user3731622
  • 4,844
  • 8
  • 45
  • 84
  • 1
    Are you aware that when you click on the radio button, and, use the SetCheck method, that you are in effect causing another OnClickedRadioButton event? – rrirower Jan 13 '15 at 14:21
  • @rrirower No, I was not aware. I'm new to MFC. Do you know a good way to allow the user to toggle the radio button? – user3731622 Jan 13 '15 at 18:09
  • You may be using the wrong control. "Toggling" usually implies a check box because it can be 'on' or 'off'. – rrirower Jan 13 '15 at 18:11
  • @rrirower Yeah, I saw that, but I want the visual of the radio button rather than the check box. – user3731622 Jan 13 '15 at 18:45
  • @rriower The radio button toggles now. However, if the button is unchecked & I click on another program, when I click on the CDialog that has the radio button, the radio button gets checked automatically. Any ideas what causes this? – user3731622 Jan 13 '15 at 19:43
  • Without seeing more, I'd say there's a bug in your code. I'd also say you're making a mistake if you're using just **one** radio button. That's not an appropriate use of that control in terms of User Interface in the context you've described. – rrirower Jan 13 '15 at 20:07

1 Answers1

0

I believe the problem was related to @rrirower comment that SetCheck will cause another OnClickedRadioButton event.

Regardless of the root cause, the quick fix to allow my implementation to toggle a radio button betweenBST_CHECKED and BST_UNCHECKED was to set the radio button's Auto property to False.

To do this: 1) Open the resource in visual studio 2) Right-click on the radio button and select Properties 3) In the Appearance section, set the Auto property to False.

Here is the overall solution to toggle a single radio button in a subclass of CDialog (assuming you already added a Dialog resource with a radio button with ID IDC_RADIO):

1) Add radio button IDC_RADIO to the message map by placing this line

ON_BN_CLICKED(IDC_RADIO, OnBnClickedRadioButton)

between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP.

2) Add the handler function to your subclass of CDialog

void OnBnClickedRadioButton()
{
    toggleButton(*(CButton*)GetDlgItem(IDC_RADIO));
}

3) Add the toggle function to your subclass of CDialog

void toggleButton(CButton& theButton)
{
    switch(theButton.GetCheck())
    {
        case BST_UNCHECKED:
        {
            theButton.SetCheck(BST_CHECKED);
            break;
        }
        case BST_CHECKED:
        {
            theButton.SetCheck(BST_UNCHECKED);
            break;
        }
        default:
        {
            theButton.SetCheck(BST_UNCHECKED);
        }
    }
}
user3731622
  • 4,844
  • 8
  • 45
  • 84