Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Simple question about radio buttons

Simple question about radio buttons

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
19 Posts 3 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jay Hova

    Hi All, I have a silly question about radio buttons that seems pretty simple but yet i can't get to work. I have a dialog with 2 radio buttons to allow the user to select one of two choices. I initially set one as default using CheckRadioButton(IDC_B1, IDC_B2, IDC_B1). I want my program to remember which one is selected by the user, so that if they open the dialog again through a pull down menu it will select the same one as the user selected before. can someone tell me how to do this exactly, i don't know what i am doing wrong. Thanks for your help.

    T Offline
    T Offline
    tmangan
    wrote on last edited by
    #2

    Short answer: The button can't do it for you. You need to retrieve the current value and save it. Then use that value when you re-create it. If you want to have it remember between sessions, you would save that in the regisitry (HKEY_CURRENT_USER PLEASE!). tim Founder, TMurgent Technologies www.tmurgent.com tmangan@tmurgent.com

    J 1 Reply Last reply
    0
    • T tmangan

      Short answer: The button can't do it for you. You need to retrieve the current value and save it. Then use that value when you re-create it. If you want to have it remember between sessions, you would save that in the regisitry (HKEY_CURRENT_USER PLEASE!). tim Founder, TMurgent Technologies www.tmurgent.com tmangan@tmurgent.com

      J Offline
      J Offline
      Jay Hova
      wrote on last edited by
      #3

      Thank you for your help. I am very new to programming...I tried to do that, but with no success. (Or at least I thought i was doing that) Can someone tell me how to save these values and then use them to re-create the buttons. I tried to make a control variable with the classwizard, but MFC doesn't let me make a control value for radio buttons. PLEASE help...i need to get this done soon. all help is appreciated.

      D 1 Reply Last reply
      0
      • J Jay Hova

        Thank you for your help. I am very new to programming...I tried to do that, but with no success. (Or at least I thought i was doing that) Can someone tell me how to save these values and then use them to re-create the buttons. I tried to make a control variable with the classwizard, but MFC doesn't let me make a control value for radio buttons. PLEASE help...i need to get this done soon. all help is appreciated.

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #4

        Unless the radio button has the WS_GROUP style, ClassWizard will not allow you create a control variable for it. Now the "tricky" part. You can have one control variable represent the entire group of radio buttons, or you can have one control variable for each radio button in the group. There are pros and cons to both. To use the registry, you can use the registry API (RegOpenKeyEx(), RegQueryValueEx(), RegSetValueEx()), or the CRegKey class.

        J 1 Reply Last reply
        0
        • D David Crow

          Unless the radio button has the WS_GROUP style, ClassWizard will not allow you create a control variable for it. Now the "tricky" part. You can have one control variable represent the entire group of radio buttons, or you can have one control variable for each radio button in the group. There are pros and cons to both. To use the registry, you can use the registry API (RegOpenKeyEx(), RegQueryValueEx(), RegSetValueEx()), or the CRegKey class.

          J Offline
          J Offline
          Jay Hova
          wrote on last edited by
          #5

          Thanks for the help David. What would you recommend in my situation. To have one for the group or one for each. I have such little experience with programming that I do not know which route to take. I am still confused to how i can use it to have it check the radio buttons when the user brings up the menu item again. I keep gettng an assertion failure. I do not need the settings to be saved for the next session. I just need it for when the app is run once. thanks in advance.

          D 1 Reply Last reply
          0
          • J Jay Hova

            Thanks for the help David. What would you recommend in my situation. To have one for the group or one for each. I have such little experience with programming that I do not know which route to take. I am still confused to how i can use it to have it check the radio buttons when the user brings up the menu item again. I keep gettng an assertion failure. I do not need the settings to be saved for the next session. I just need it for when the app is run once. thanks in advance.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #6

            Jay Hova wrote: What would you recommend in my situation. To have one for the group or one for each. I have such little experience with programming that I do not know which route to take. I am still confused to how i can use it to have it check the radio buttons when the user brings up the menu item again. I keep gettng an assertion failure. Let's operate on the premise that one variable represents the entire group (two radio buttons in your case). If this variable is 0, then the first radio button in the group will be checked. If this variable is 1, then the second radio button in the group will be checked. See the pattern? If this value is set in the dialog's constructor, nothing else needs to be done. If this value is set in the dialog's OnInitDialog() method, a call to UpdateData(FALSE) will be needed. Read here for some more details: http://flounder.com/getdlgitem.htm#Radio button variables Jay Hova wrote: I do not need the settings to be saved for the next session. I just need it for when the app is run once. Then the owner of the dialog instance should store the value. Is this an SDI or an MDI application?

            J 1 Reply Last reply
            0
            • D David Crow

              Jay Hova wrote: What would you recommend in my situation. To have one for the group or one for each. I have such little experience with programming that I do not know which route to take. I am still confused to how i can use it to have it check the radio buttons when the user brings up the menu item again. I keep gettng an assertion failure. Let's operate on the premise that one variable represents the entire group (two radio buttons in your case). If this variable is 0, then the first radio button in the group will be checked. If this variable is 1, then the second radio button in the group will be checked. See the pattern? If this value is set in the dialog's constructor, nothing else needs to be done. If this value is set in the dialog's OnInitDialog() method, a call to UpdateData(FALSE) will be needed. Read here for some more details: http://flounder.com/getdlgitem.htm#Radio button variables Jay Hova wrote: I do not need the settings to be saved for the next session. I just need it for when the app is run once. Then the owner of the dialog instance should store the value. Is this an SDI or an MDI application?

              J Offline
              J Offline
              Jay Hova
              wrote on last edited by
              #7

              David, Thanks for your help.... I am going to read that site you provided now. Going by what you wrote, can i check the radio button using SetCheck? I tried just setting the variable equal to one and it doesn't check either one.... Sorry for being so slow with this. Its a MDI app btw. many thanks...

              D 1 Reply Last reply
              0
              • J Jay Hova

                David, Thanks for your help.... I am going to read that site you provided now. Going by what you wrote, can i check the radio button using SetCheck? I tried just setting the variable equal to one and it doesn't check either one.... Sorry for being so slow with this. Its a MDI app btw. many thanks...

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #8

                Jay Hova wrote: Going by what you wrote, can i check the radio button using SetCheck? Yes, this assumes that you have one control variable per radio button. In the dialog's OnInitDialog() method, you'd have something like:

                The m_first_radio_button and m_second_radio_button variables are of type CButton, not integer
                if (0 == some_variable)
                m_first_radio_button.SetCheck(BST_CHECKED);
                else if (1 == some_variable)
                m_second_radio_button.SetCheck(BST_CHECKED);
                else
                ASSERT(FALSE);

                Jay Hova wrote: I tried just setting the variable equal to one and it doesn't check either one.... This sounds like an entry is missing from the dialog's DoDataExchange() method, or you set the value in the wrong location.

                J 1 Reply Last reply
                0
                • D David Crow

                  Jay Hova wrote: Going by what you wrote, can i check the radio button using SetCheck? Yes, this assumes that you have one control variable per radio button. In the dialog's OnInitDialog() method, you'd have something like:

                  The m_first_radio_button and m_second_radio_button variables are of type CButton, not integer
                  if (0 == some_variable)
                  m_first_radio_button.SetCheck(BST_CHECKED);
                  else if (1 == some_variable)
                  m_second_radio_button.SetCheck(BST_CHECKED);
                  else
                  ASSERT(FALSE);

                  Jay Hova wrote: I tried just setting the variable equal to one and it doesn't check either one.... This sounds like an entry is missing from the dialog's DoDataExchange() method, or you set the value in the wrong location.

                  J Offline
                  J Offline
                  Jay Hova
                  wrote on last edited by
                  #9

                  David, That is exactly as i was doing it in the OnInitDialog.... Perhaps I am doing something incorrect in the OnOk. How would i get the which one is selected by the user? thanks again for all your help and patience with a mechanical engineer with no wit to program. ;P

                  D 1 Reply Last reply
                  0
                  • J Jay Hova

                    David, That is exactly as i was doing it in the OnInitDialog.... Perhaps I am doing something incorrect in the OnOk. How would i get the which one is selected by the user? thanks again for all your help and patience with a mechanical engineer with no wit to program. ;P

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #10

                    Jay Hova wrote: Perhaps I am doing something incorrect in the OnOk. How would i get the which one is selected by the user?

                    if (xxx.GetCheck() == BST_CHECKED)
                    ...

                    J 1 Reply Last reply
                    0
                    • D David Crow

                      Jay Hova wrote: Perhaps I am doing something incorrect in the OnOk. How would i get the which one is selected by the user?

                      if (xxx.GetCheck() == BST_CHECKED)
                      ...

                      J Offline
                      J Offline
                      Jay Hova
                      wrote on last edited by
                      #11

                      This is what i have in OnOk():

                      if(m_Screen_Radio.GetCheck() == BST_CHECKED)
                           m_SendTo = 0;
                      else
                           m_SendTo = 1;
                      

                      where m_SendTo is a member variable of type int and m_Screen_Radio is CButton

                      D 1 Reply Last reply
                      0
                      • J Jay Hova

                        This is what i have in OnOk():

                        if(m_Screen_Radio.GetCheck() == BST_CHECKED)
                             m_SendTo = 0;
                        else
                             m_SendTo = 1;
                        

                        where m_SendTo is a member variable of type int and m_Screen_Radio is CButton

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #12

                        What you've shown should work. Does it? Now in the code that instantiates the dialog object, you'll need something like:

                        CMyDialog dlg;
                        dlg.m_SendTo = m_SendTo;
                        if (dlg.DoModal() == IDOK)
                        m_SendTo = dlg.m_SendTo;

                        Make sense?

                        J 1 Reply Last reply
                        0
                        • D David Crow

                          What you've shown should work. Does it? Now in the code that instantiates the dialog object, you'll need something like:

                          CMyDialog dlg;
                          dlg.m_SendTo = m_SendTo;
                          if (dlg.DoModal() == IDOK)
                          m_SendTo = dlg.m_SendTo;

                          Make sense?

                          J Offline
                          J Offline
                          Jay Hova
                          wrote on last edited by
                          #13

                          I get an assertion failure with that code that I had.... You lost me however with what you wrote. I don't understand it. Where should I put that code? Please clarify if you can. Many many thanks.

                          D 1 Reply Last reply
                          0
                          • J Jay Hova

                            I get an assertion failure with that code that I had.... You lost me however with what you wrote. I don't understand it. Where should I put that code? Please clarify if you can. Many many thanks.

                            D Offline
                            D Offline
                            David Crow
                            wrote on last edited by
                            #14

                            Jay Hova wrote: I get an assertion failure with that code that I had.... See my previous comment about DoDataExchange(). Even though you have a CButton control variable, there must be an entry in DoDataExchange() for it. To verify, note the file/line of the fired assertion. If you then look at that code, it will most likely look something like: ASSERT(::IsWindow(m_hWnd)); which means there is no window associated with the variable. Jay Hova wrote: You lost me however with what you wrote. I don't understand it. Where should I put that code? Please clarify if you can. You previously mentioned that the dialog was being displayed as the result of a menu selection. I assume you already have a call to DoModal() in there. Correct?

                            J 1 Reply Last reply
                            0
                            • D David Crow

                              Jay Hova wrote: I get an assertion failure with that code that I had.... See my previous comment about DoDataExchange(). Even though you have a CButton control variable, there must be an entry in DoDataExchange() for it. To verify, note the file/line of the fired assertion. If you then look at that code, it will most likely look something like: ASSERT(::IsWindow(m_hWnd)); which means there is no window associated with the variable. Jay Hova wrote: You lost me however with what you wrote. I don't understand it. Where should I put that code? Please clarify if you can. You previously mentioned that the dialog was being displayed as the result of a menu selection. I assume you already have a call to DoModal() in there. Correct?

                              J Offline
                              J Offline
                              Jay Hova
                              wrote on last edited by
                              #15

                              I tried to implement the code you wrote, but i get an error saying that m_SendTo is an undeclared identifier. The assertion failure is in afxwin2.inl on line 588.... Thanks again for your help. Your patience is amazing! :-O

                              D 1 Reply Last reply
                              0
                              • J Jay Hova

                                I tried to implement the code you wrote, but i get an error saying that m_SendTo is an undeclared identifier. The assertion failure is in afxwin2.inl on line 588.... Thanks again for your help. Your patience is amazing! :-O

                                D Offline
                                D Offline
                                David Crow
                                wrote on last edited by
                                #16

                                Jay Hova wrote: I tried to implement the code you wrote, but i get an error saying that m_SendTo is an undeclared identifier. Right. You'll need to add this member variable to the appropriate class. Jay Hova wrote: The assertion failure is in afxwin2.inl on line 588.... As I predicted:

                                _AFXWIN_INLINE int CButton::GetCheck() const
                                { ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, BM_GETCHECK, 0, 0); }

                                J 1 Reply Last reply
                                0
                                • D David Crow

                                  Jay Hova wrote: I tried to implement the code you wrote, but i get an error saying that m_SendTo is an undeclared identifier. Right. You'll need to add this member variable to the appropriate class. Jay Hova wrote: The assertion failure is in afxwin2.inl on line 588.... As I predicted:

                                  _AFXWIN_INLINE int CButton::GetCheck() const
                                  { ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, BM_GETCHECK, 0, 0); }

                                  J Offline
                                  J Offline
                                  Jay Hova
                                  wrote on last edited by
                                  #17

                                  David, Sorry that you have to spoon feed me through this. I feel like such a moron because I can not grasp this concept. I really appreciate your help. What do i have to put in DoDataExchange? Also by adding the member variable to the other cpp file, won't it over write the existing data in m_SendTo? Thanks.

                                  D 1 Reply Last reply
                                  0
                                  • J Jay Hova

                                    David, Sorry that you have to spoon feed me through this. I feel like such a moron because I can not grasp this concept. I really appreciate your help. What do i have to put in DoDataExchange? Also by adding the member variable to the other cpp file, won't it over write the existing data in m_SendTo? Thanks.

                                    D Offline
                                    D Offline
                                    David Crow
                                    wrote on last edited by
                                    #18

                                    Jay Hova wrote: Sorry that you have to spoon feed me through this. I feel like such a moron because I can not grasp this concept. I really appreciate your help. Not a problem. The first few applications are always the hardest. Jay Hova wrote: What do i have to put in DoDataExchange? Also by adding the member variable to the other cpp file, won't it over write the existing data in m_SendTo? I've never had to manaully add anything to DoDataExchange(). ClassWizard does it all for me (hint: use CW to create the member and control variables). By looking at that function, however, you can get a feel for what MFC might be doing. Jay Hova wrote: Also by adding the member variable to the other cpp file, won't it over write the existing data in m_SendTo? I'm not quite sure I understand what you are asking here. Care to elaborate?

                                    J 1 Reply Last reply
                                    0
                                    • D David Crow

                                      Jay Hova wrote: Sorry that you have to spoon feed me through this. I feel like such a moron because I can not grasp this concept. I really appreciate your help. Not a problem. The first few applications are always the hardest. Jay Hova wrote: What do i have to put in DoDataExchange? Also by adding the member variable to the other cpp file, won't it over write the existing data in m_SendTo? I've never had to manaully add anything to DoDataExchange(). ClassWizard does it all for me (hint: use CW to create the member and control variables). By looking at that function, however, you can get a feel for what MFC might be doing. Jay Hova wrote: Also by adding the member variable to the other cpp file, won't it over write the existing data in m_SendTo? I'm not quite sure I understand what you are asking here. Care to elaborate?

                                      J Offline
                                      J Offline
                                      Jay Hova
                                      wrote on last edited by
                                      #19

                                      David, Thank you for your help. I had to manually insert the control in DoDataExchange because after reading the article you linked me to earlier today, i ungrouped the second radio. It doesn't make sense to me, but it seems to be working right now. Now to the next part of this app.... Many many thanks for your help, patience, and understanding. You are really a class act guy!

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups