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. Radio Button's Satus??

Radio Button's Satus??

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++visual-studio
14 Posts 5 Posters 0 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.
  • A AmbiguousName

    hello guys...im new to MFC using VS 2008. How can I get the radio buttons status in MFC using VS 2008??:confused:

    R Offline
    R Offline
    rp_suman
    wrote on last edited by
    #3

    BOOL bSelected = (CButton*) GetDlgItem(IDC_XYZ))->GetCheck();

    -- "Programming is an art that fights back!"

    M 1 Reply Last reply
    0
    • R rp_suman

      BOOL bSelected = (CButton*) GetDlgItem(IDC_XYZ))->GetCheck();

      -- "Programming is an art that fights back!"

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #4

      GetCheck returns an int in case the radio or more probably the checkbox is a 3 state button; so if you only want to see if it's checked, do something like :

      BOOL bSelected = (CButton*) GetDlgItem(IDC_XYZ))->GetCheck() == BST_CHECKED;

      (but I'm only nitpicking).

      Watched code never compiles.

      L 1 Reply Last reply
      0
      • M Maximilien

        GetCheck returns an int in case the radio or more probably the checkbox is a 3 state button; so if you only want to see if it's checked, do something like :

        BOOL bSelected = (CButton*) GetDlgItem(IDC_XYZ))->GetCheck() == BST_CHECKED;

        (but I'm only nitpicking).

        Watched code never compiles.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        Maximilien wrote:

        BOOL bSelected = (CButton*) GetDlgItem(IDC_XYZ))->GetCheck() == BST_CHECKED; (but I'm only nitpicking).

        While we are picking nits... GetDlgItem() actually returns a pointer to a CTempWnd* object if there is no entry in the internal MFC permanent CHandleMap. The only reason why this code does not cause an access violation is because CButton::GetCheck() is an __inlined SendMessage(m_hWnd,BM_GETCHECK... You should avoid upcasting a pointer returned from GetDlgItem(). Best Wishes, -David Delaune

        L 1 Reply Last reply
        0
        • L Lost User

          Maximilien wrote:

          BOOL bSelected = (CButton*) GetDlgItem(IDC_XYZ))->GetCheck() == BST_CHECKED; (but I'm only nitpicking).

          While we are picking nits... GetDlgItem() actually returns a pointer to a CTempWnd* object if there is no entry in the internal MFC permanent CHandleMap. The only reason why this code does not cause an access violation is because CButton::GetCheck() is an __inlined SendMessage(m_hWnd,BM_GETCHECK... You should avoid upcasting a pointer returned from GetDlgItem(). Best Wishes, -David Delaune

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          Randor wrote:

          You should avoid upcasting a pointer returned from GetDlgItem().

          To add to the nitpicking that is exactly what the documentation[^] recommends that you do.

          It's time for a new signature.

          L 1 Reply Last reply
          0
          • L Lost User

            Randor wrote:

            You should avoid upcasting a pointer returned from GetDlgItem().

            To add to the nitpicking that is exactly what the documentation[^] recommends that you do.

            It's time for a new signature.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            Richard MacCutchan wrote:

            that is exactly what the documentation[^] recommends that you do.

            An additional nit to pick: The upcast in the documentation is completely pointless... GotoDlgCtrl[^] takes a pointer to a CWnd* object. The CWnd* pointer returned by GetDlgItem would suffice. The MSDN documentation is full of bad sample codes. Best Wishes, -David Delaune

            L 1 Reply Last reply
            0
            • L Lost User

              Richard MacCutchan wrote:

              that is exactly what the documentation[^] recommends that you do.

              An additional nit to pick: The upcast in the documentation is completely pointless... GotoDlgCtrl[^] takes a pointer to a CWnd* object. The CWnd* pointer returned by GetDlgItem would suffice. The MSDN documentation is full of bad sample codes. Best Wishes, -David Delaune

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              Randor wrote:

              The upcast in the documentation is completely pointless

              I disagree; the upcast makes it clear what the code is trying to do. Since the actual item being returned is a CButton (even though GetDlgItem doesn't know that) it would seem the sensible thing to do, especially if you then want to call some method on the CButton object.

              It's time for a new signature.

              L 1 Reply Last reply
              0
              • L Lost User

                Randor wrote:

                The upcast in the documentation is completely pointless

                I disagree; the upcast makes it clear what the code is trying to do. Since the actual item being returned is a CButton (even though GetDlgItem doesn't know that) it would seem the sensible thing to do, especially if you then want to call some method on the CButton object.

                It's time for a new signature.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #9

                Richard MacCutchan wrote:

                Since the actual item being returned is a CButton

                Are you sure saying that GetDlgItem returns a CButton object? Best Wishes, -David Delaune

                L 1 Reply Last reply
                0
                • A AmbiguousName

                  hello guys...im new to MFC using VS 2008. How can I get the radio buttons status in MFC using VS 2008??:confused:

                  B Offline
                  B Offline
                  bolivar123
                  wrote on last edited by
                  #10

                  How about calling GetCheckedRadioButton like:

                  int radioCheck = GetCheckedRadioButton(ID_RADIO1,ID_RADIO3);

                  This will return the ID of the checked radio button or 0 if none are checked

                  1 Reply Last reply
                  0
                  • L Lost User

                    Richard MacCutchan wrote:

                    Since the actual item being returned is a CButton

                    Are you sure saying that GetDlgItem returns a CButton object? Best Wishes, -David Delaune

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #11

                    Randor wrote:

                    Are you sure saying that GetDlgItem returns a CButton object?

                    No, I'm saying it returns the object whose ID is sent to GetDlgItem(), which will be some class derived from CWnd. That is why the upcast is required.

                    It's time for a new signature.

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      Randor wrote:

                      Are you sure saying that GetDlgItem returns a CButton object?

                      No, I'm saying it returns the object whose ID is sent to GetDlgItem(), which will be some class derived from CWnd. That is why the upcast is required.

                      It's time for a new signature.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      Richard MacCutchan wrote:

                      No, I'm saying it returns the object whose ID is sent to GetDlgItem(), which will be some class derived from CWnd.

                      Richard, This is not always the case. GetDlgItem will return a CTempWnd object[^] if there is no entry inside the internal MFC handle map. You should always avoid upcasting a pointer from GetDlgItem. Best Wishes, -David Delaune

                      L 1 Reply Last reply
                      0
                      • L Lost User

                        Richard MacCutchan wrote:

                        No, I'm saying it returns the object whose ID is sent to GetDlgItem(), which will be some class derived from CWnd.

                        Richard, This is not always the case. GetDlgItem will return a CTempWnd object[^] if there is no entry inside the internal MFC handle map. You should always avoid upcasting a pointer from GetDlgItem. Best Wishes, -David Delaune

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #13

                        Sorry but I'm still not clear on this. It doesn't matter whether the object pointer is permanent or temporary, it is still a CButton*. If the object does not exist, or the value to GetDlgItem() is not valid you won't get a pointer to anything. OK David, I have just read that article again and understand (almost, but not completely) why your assertion was correct. Sorry, for belabouring this but my experience of MFC was that this upcasting always worked; I just hope nobody is still using one of my old programs. Thanks for the guidance. Richard

                        It's time for a new signature.

                        modified on Tuesday, July 20, 2010 1:28 PM

                        L 1 Reply Last reply
                        0
                        • L Lost User

                          Sorry but I'm still not clear on this. It doesn't matter whether the object pointer is permanent or temporary, it is still a CButton*. If the object does not exist, or the value to GetDlgItem() is not valid you won't get a pointer to anything. OK David, I have just read that article again and understand (almost, but not completely) why your assertion was correct. Sorry, for belabouring this but my experience of MFC was that this upcasting always worked; I just hope nobody is still using one of my old programs. Thanks for the guidance. Richard

                          It's time for a new signature.

                          modified on Tuesday, July 20, 2010 1:28 PM

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #14

                          Richard MacCutchan wrote:

                          Thanks for the guidance.

                          Your welcome. :) Best Wishes, -David Delaune

                          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