CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2006
    Posts
    9

    radio button problem

    in my program i have a dialog that contains a group of radio buttons.

    everytime you make a radio button selection, a messagebox appears asking the user whether or not they want to confirm this selection.

    the problem i am experiencing is when the user tries to cancel the selection.

    for example, in a handler for one the of the radio buttons:

    Code:
    void radioBtnDlg::OnBnClickedRadio1()
    {
    	// TODO: Add your control notification handler code here
    
    	if(MessageBox(MSG_0, MSG_1,  MB_OKCANCEL) == IDOK)
    	{
    		// get data from dialog selection.
    		UpdateData(TRUE);
    	}
    	else
    	{
    		// need to return dialog to previous state.
    		UpdateData(FALSE);
    	}
    }
    after selecting cancel, if you then click the main window to unset the focus from the dialog, then set focus back to the dialog, this handler is called again! So then the messagebox keeps re-appearing.

    Anyone know why this is occurring? Am i doing something wrong?

    Thank you for your help.

  2. #2
    Join Date
    Feb 2006
    Posts
    9

    Re: radio button problem

    and here is a sample project (see attached).

    it is a very simple app that just points out the problem i am experiencing.

    make a radio button selection, then when a messagebox appears, click cancel to cancel the selection. then click on the main window to set focus to the main window, then click on the dialog window to set focus back to the dialog window. the messagebox re-appears because the handler for the radio button was called again!
    Attached Files Attached Files

  3. #3
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: radio button problem

    Try SetFocus(NULL) to remove the focus from that radio button you clicked.

    Regards / Z

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: radio button problem

    Don't make it that complex. UpdateData should not bet called until and unless user has pressed Apply or OK button. Your program should store the last "selection" (as a variable within the class).

    For instance you have 3 radio buttons, and you want to confirm only for third button selection (let assume that selection is critical). So when The message handler is called for that selection, you should do something like:
    Code:
     if ( confirm_from_user != IDOK)
    {
    // Restore last selection by calling CheckRadioButton
    }
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  5. #5
    Join Date
    Feb 2006
    Posts
    9

    Re: radio button problem

    thanks for the reponses.

    ajay, i tried your method to avoid the call to updatedata(false) if user cancels, and instead manually set radio button states to what they were with checkradiobutton. however, the problem is still there.

    i think my call to updatedata(false) is no different then manually setting radio buttons. i have a member variable in dodataexchange tied to this groups of buttons. if user cancels selection, that member variable already contains the previous selected state, so by calling updatedata(false), i would think that should simply update the radio buttons to the value of the member variable (which already has previous state).

    in the 2nd post of this thread, i uploaded the sample app. kudos to someone who can find a solution. i found a solution but it is a little bit wierd and trying to find a better solution.

  6. #6
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: radio button problem

    I've not looked at your project, neither I think I should need it.

    The basic problem is: bad User Interface design.

    You are calling UpdateData(FALSE), just to restore the previous value. What about the other controls your dialog may have (I assume).
    For example there is a text box which is empty by default, combo box that shows 10 items. Now if user enters "Codeguru.com" in editbox, and also selects 4th item from combobox. Further to this, he/she selects this radio button.

    Jeez! All the controls are now restored to default values (empty text box and 1st item selected in combo) - just because user has "tried" to select a radio box option???

    One more important thing: I am not saying about the regular variable for radio button, that is referenced in DoDataExchange - it is related to UpdateData. Do have a variable that keeps track of current user selection (just like Edit box contents are stored somewhere).
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  7. #7
    Join Date
    Feb 2006
    Posts
    9

    Re: radio button problem

    ajay, you have a good point about the updatedata. but if i understood your solution you suggested earlier, it is to not use updatedata and instead manually set the radio button state directly. well, i tried that and it didn't solve the problem either. but anyways, thanks for your input.
    Last edited by kashikoi; June 8th, 2006 at 02:50 PM.

  8. #8
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: radio button problem

    Code:
    if(MessageBox(MSG_0, MSG_1,  MB_OKCANCEL) == IDOK)
    	{
    		// get data from dialog selection.
    		UpdateData(TRUE);
    	}
    	else
    	{
    		// need to return dialog to previous state.
    		UpdateData(FALSE);
    	}
    See, in this case UpdateData is changing the "state" of radio button. Due to the change, handler is called. It does not matter how 'state' is changed: by end user, DoDataExchange, CheckRadioButton, SendMessage....

    Please read my previous post, and try to grasp it!
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  9. #9
    Join Date
    Feb 2006
    Posts
    9

    Re: radio button problem

    ajay, so regardless what method i use to change state of the radio button (updata(false), checkradiobutton, etc...), handler for radio button is called?

    the problem i am seeing here is this:

    user selects radio button..

    radio button state has now changed..

    this will cause handler for the radio button to be called..

    message box is displayed..

    user selects cancel..

    now here is the problem. since the user already made that first selection (which is why we are here at this point in the program), how is it possible to change the radio button state back to what it was before the user made that initial selection, without causing handler to be called again..

    since the initial implication is that handler is called whenever radio button state changes, then this appears to be impossible.

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: radio button problem

    Set a flag:
    Code:
    void radioBtnDlg::OnBnClickedRadio1()
    {
       static bool bDontDoAnything = false;
       if (!bDontDoAnything)
       {
    	// TODO: Add your control notification handler code here
    
    	if(MessageBox(MSG_0, MSG_1,  MB_OKCANCEL) == IDOK)
    	{
    		// get data from dialog selection.
    		UpdateData(TRUE);
    	}
    	else
    	{
    		// need to return dialog to previous state.
                    bDontDoAnything  = true;
    		UpdateData(FALSE);
                    bDontDoAnything  = false;
    	}
       }
    }
    Viggy

  11. #11
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: radio button problem

    if (!bDontDoAnything)
    Should it not be:
    Code:
     if (bDontDoAnything)
    Well, Viggy, I would say: This is another example of BAD DESIGN!!
    Last edited by Ajay Vijay; June 9th, 2006 at 10:30 AM.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  12. #12
    Join Date
    Feb 2002
    Posts
    4,640

    Re: radio button problem

    Well, yeah. It does work around the issue.

    Personally, I'd handle this in a custom DDX/DDV function, and let the framwork worry about resetting states.

    Viggy

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