MFC Radio button handler called multiple times.

Posted on 2009-04-25
Last Modified: 2013-11-20
I am using 2 radio buttons.   BUTTON1-Group property set.
I have ON_BN_CLICKED handlers say BUTTON1_Handler(), BUTTON2_Handler().

After clcking on any of the two radio buttons, I display a message box with MB_YESNO style and get confirmation from the user. If user chooses "NO", I have to revert the selection.

If I try to change the state of BUTTON1 using "SetCheck()" inside BUTTON1_Handler(), then BUTTON1_handler() is called multiple times and the messge box is popped up again and again.

Please help me how to change the radio button state inside its own handler...



       if( IDNO == AfxMessageBox("Confirm ?",MB_YESNO) )








       if( IDNO == AfxMessageBox("Confirm ?",MB_YESNO) )






Open in new window

Question by:arun123prasad
    LVL 49

    Accepted Solution

    Here is the MFC way to handle a group of radio buttons automatically -- you should never have to do SetCheck()

    In the dialog editor, make sure the tab-order of the radio buttons are sequential.
    Then in only the first radio button, set the Group checkbox in its properties.
    Then right-click that first button and press Ctrl+W (class wizard)
    In the Variables page, only the first radio button is listed. Give it a variable name, say,
    This will add a DDX_Radio(...) to the DoDataExchange for the dlg.

    Now before you open the dialog, set the member variable to indicate which radio button is set (0...n-1). For instance:

    CMyDialog dlg;
    dlg.m_nRadioGrpSel= 0; // pre-select the first Rb
    if ( IDOK == dlg.DoModal() ) {
        if ( dlg.m_nRadioGrpSel==2 ) {
            // the third Rb was selected

    If you set...
    dlg.m_nRadioGrpSel= -1; // or other invalid value

    then none of the radio buttons will be selected when the user first sees the dialog box. Furthermore, the user will not be able to use the Tab key to get to any of the radio buttons in the group (so he can't accidentally select one except by using the mouse).
    So we have a way to determine not just the status of individual buttons but the group as a whole, and we can set the desired option on screen by just putting a number (0,1,2...) into a member variable and then using

    And conversely, we can set the variable to match the current selection by using:
    UpdateData( TRUE )
    I think the comments in the attached code should cover it.

    void CD55Dlg::DoDataExchange(CDataExchange* pDX)
    	DDX_Radio(pDX, IDC_RADIO1, m_nRadioGrpSel);  // <<--- Note
    	ON_BN_CLICKED(IDC_RADIO1, OnRadioGrpClick) // << note: same fn
    	ON_BN_CLICKED(IDC_RADIO2, OnRadioGrpClick)
    	ON_BN_CLICKED(IDC_RADIO3, OnRadioGrpClick)
    void CD55Dlg::OnRadioGrpClick() 
    	int nCurOption= m_nRadioGrpSel;
    	UpdateData(TRUE);  // collect the (possibly new) value
    	if ( nCurOption != m_nRadioGrpSel ) { // user requested to change the settings
    	int nResp= AfxMessageBox("Confirm ?",MB_YESNO);
    		 if ( nResp == IDNO ) {
    			m_nRadioGrpSel= nCurOption;  // revert to earlier value
    			UpdateData( FALSE );         

    Open in new window

    LVL 43

    Assisted Solution

    You need a boolean flag to prevent the messagebox when you set the radio programatically.
    eg (pseudo code).

    bool m_bDisplay = false;

      if (m_bDisplay)
    m_bDisplay = true;
    m_bDisplay = false;

    and the same for handler2

    LVL 49

    Expert Comment

    Next time, I shall strive to work harder and provide more information, in hopes that I can earn an A.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now