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. Mapping array of CButtons

Mapping array of CButtons

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
14 Posts 4 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.
  • D Offline
    D Offline
    dart13
    wrote on last edited by
    #1

    How to map array of CButtons to button resources on Dialog form?

    D 1 Reply Last reply
    0
    • D dart13

      How to map array of CButtons to button resources on Dialog form?

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

      Have you looked at CMap?


      "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

      D 1 Reply Last reply
      0
      • D David Crow

        Have you looked at CMap?


        "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

        D Offline
        D Offline
        dart13
        wrote on last edited by
        #3

        I looked in MSDN, but I'm not quite sure if I know how to use it in this case.

        D 1 Reply Last reply
        0
        • D dart13

          I looked in MSDN, but I'm not quite sure if I know how to use it in this case.

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

          dart13 wrote: I looked in MSDN, but I'm not quite sure if I know how to use it... "It" being MSDN or the CMap class?


          "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

          D 1 Reply Last reply
          0
          • D David Crow

            dart13 wrote: I looked in MSDN, but I'm not quite sure if I know how to use it... "It" being MSDN or the CMap class?


            "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

            D Offline
            D Offline
            dart13
            wrote on last edited by
            #5

            It being CMap. Sorry for misunderstanding.

            D 1 Reply Last reply
            0
            • D dart13

              It being CMap. Sorry for misunderstanding.

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

              Something like:

              CMap<int,int,CButton*,CButton*> mapButtons;

              mapButtons.SetAt(123, &m_btn1);
              mapButtons.SetAt(456, &m_btn2);
              mapButtons.SetAt(789, &m_btn3);

              CButton *btn;
              BOOL bFound;
              bFound = mapButtons.Lookup(123, btn);
              bFound = mapButtons.Lookup(234, btn);

              Think of a map as a cross-reference.


              "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

              D 1 Reply Last reply
              0
              • D David Crow

                Something like:

                CMap<int,int,CButton*,CButton*> mapButtons;

                mapButtons.SetAt(123, &m_btn1);
                mapButtons.SetAt(456, &m_btn2);
                mapButtons.SetAt(789, &m_btn3);

                CButton *btn;
                BOOL bFound;
                bFound = mapButtons.Lookup(123, btn);
                bFound = mapButtons.Lookup(234, btn);

                Think of a map as a cross-reference.


                "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

                D Offline
                D Offline
                dart13
                wrote on last edited by
                #7

                Thanks for your help, but shoudn't DoDataExchange() be in some way involved in this? I have an array CButton btns[10] that need to be 'connected' to 10 buttons on a dialog form. If I use ClassWizard (Member Variables) I can map each button with a single CButton variable, but it doesn't allow me to use btns[0], for example, as a name of a variable.

                B D 2 Replies Last reply
                0
                • D dart13

                  Thanks for your help, but shoudn't DoDataExchange() be in some way involved in this? I have an array CButton btns[10] that need to be 'connected' to 10 buttons on a dialog form. If I use ClassWizard (Member Variables) I can map each button with a single CButton variable, but it doesn't allow me to use btns[0], for example, as a name of a variable.

                  B Offline
                  B Offline
                  Blake Miller
                  wrote on last edited by
                  #8

                  You can bypass ClassWizard and code assignments in DoDataExchange by hand. Just leave your 'manual' assignments out of the special code section delimited by //{{AFX_DATA_MAP() //}}AFX_DATA_MAP I do this all the time for custom DDV routines not built into ClassWizard or to make special assignment like you need. You can try: //{{AFX_DATA_MAP() //}}AFX_DATA_MAP DDX_Control(pDX, ID_BTN_0, btns[0]); DDX_Control(pDX, ID_BTN_1, btns[1]); ... And that should work. ClassWizard is the only thing that requires each button variable to be a separate member variable, the DDX_Control function does not care.

                  D D 3 Replies Last reply
                  0
                  • D dart13

                    Thanks for your help, but shoudn't DoDataExchange() be in some way involved in this? I have an array CButton btns[10] that need to be 'connected' to 10 buttons on a dialog form. If I use ClassWizard (Member Variables) I can map each button with a single CButton variable, but it doesn't allow me to use btns[0], for example, as a name of a variable.

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

                    I misunderstood your initial question. I think Blake Miller has given you the correct answer.


                    "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

                    1 Reply Last reply
                    0
                    • B Blake Miller

                      You can bypass ClassWizard and code assignments in DoDataExchange by hand. Just leave your 'manual' assignments out of the special code section delimited by //{{AFX_DATA_MAP() //}}AFX_DATA_MAP I do this all the time for custom DDV routines not built into ClassWizard or to make special assignment like you need. You can try: //{{AFX_DATA_MAP() //}}AFX_DATA_MAP DDX_Control(pDX, ID_BTN_0, btns[0]); DDX_Control(pDX, ID_BTN_1, btns[1]); ... And that should work. ClassWizard is the only thing that requires each button variable to be a separate member variable, the DDX_Control function does not care.

                      D Offline
                      D Offline
                      dart13
                      wrote on last edited by
                      #10

                      thanx for your help. I appreciate it.

                      1 Reply Last reply
                      0
                      • B Blake Miller

                        You can bypass ClassWizard and code assignments in DoDataExchange by hand. Just leave your 'manual' assignments out of the special code section delimited by //{{AFX_DATA_MAP() //}}AFX_DATA_MAP I do this all the time for custom DDV routines not built into ClassWizard or to make special assignment like you need. You can try: //{{AFX_DATA_MAP() //}}AFX_DATA_MAP DDX_Control(pDX, ID_BTN_0, btns[0]); DDX_Control(pDX, ID_BTN_1, btns[1]); ... And that should work. ClassWizard is the only thing that requires each button variable to be a separate member variable, the DDX_Control function does not care.

                        D Offline
                        D Offline
                        David Fleming
                        wrote on last edited by
                        #11

                        That's a good start, but I need to know how then do I map all of the buttons in the array to call a single function when clicked that can tell which button was pressed. Basically, my problem is this: I am writing a little program for testing very simple math. The program puts up a simple math problem such as "2 + 1 =", and the user is to press a button with the correct number as the answer, in this case "3". There are several of these buttons. Each should call essentially the same routine which just checks to see if the number on the button is the correct answer or not. How do I set these up as an array such that pressing any of them calls this one function with an Index in the array so that I know which one was pressed? I'd sure like to avoid having a separate function for each of these buttons because I'm planning on having about 20 of them. Thanks.

                        1 Reply Last reply
                        0
                        • B Blake Miller

                          You can bypass ClassWizard and code assignments in DoDataExchange by hand. Just leave your 'manual' assignments out of the special code section delimited by //{{AFX_DATA_MAP() //}}AFX_DATA_MAP I do this all the time for custom DDV routines not built into ClassWizard or to make special assignment like you need. You can try: //{{AFX_DATA_MAP() //}}AFX_DATA_MAP DDX_Control(pDX, ID_BTN_0, btns[0]); DDX_Control(pDX, ID_BTN_1, btns[1]); ... And that should work. ClassWizard is the only thing that requires each button variable to be a separate member variable, the DDX_Control function does not care.

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

                          OK, I figured out how to map the buttons all to the same function. Here's the code: BEGIN_MESSAGE_MAP(CButtonArrayTestDlg, CDialog) //{{AFX_MSG_MAP(CButtonArrayTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_BUTTON1, OnButtonArray) ON_BN_CLICKED(IDC_BUTTON2, OnButtonArray) ON_BN_CLICKED(IDC_BUTTON3, OnButtonArray) END_MESSAGE_MAP() Now, when each of the buttons is pressed, it does call the correct function. But now how do I know which button was pressed?

                          B 1 Reply Last reply
                          0
                          • D David Fleming

                            OK, I figured out how to map the buttons all to the same function. Here's the code: BEGIN_MESSAGE_MAP(CButtonArrayTestDlg, CDialog) //{{AFX_MSG_MAP(CButtonArrayTestDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(IDC_BUTTON1, OnButtonArray) ON_BN_CLICKED(IDC_BUTTON2, OnButtonArray) ON_BN_CLICKED(IDC_BUTTON3, OnButtonArray) END_MESSAGE_MAP() Now, when each of the buttons is pressed, it does call the correct function. But now how do I know which button was pressed?

                            B Offline
                            B Offline
                            Blake Miller
                            wrote on last edited by
                            #13

                            Maybe you need to try this instead: ON_CONTROL_RANGE(BN_CLICKED, IDC_BUTTON1, IDC_BUTTON3, OnButtonArray) Note: The range of command IDs must be contiguous. Handler functions for single commands normally take no parameters. With the exception of update handler functions, handler functions for message-map ranges require an extra parameter, nID, of type UINT. This parameter is the first parameter. The extra parameter accommodates the extra command ID needed to specify which command the user actually chose. afx_msg void OnButtonArray( UINT nID ); When called, nID will be the ID of the button pressed.

                            D 1 Reply Last reply
                            0
                            • B Blake Miller

                              Maybe you need to try this instead: ON_CONTROL_RANGE(BN_CLICKED, IDC_BUTTON1, IDC_BUTTON3, OnButtonArray) Note: The range of command IDs must be contiguous. Handler functions for single commands normally take no parameters. With the exception of update handler functions, handler functions for message-map ranges require an extra parameter, nID, of type UINT. This parameter is the first parameter. The extra parameter accommodates the extra command ID needed to specify which command the user actually chose. afx_msg void OnButtonArray( UINT nID ); When called, nID will be the ID of the button pressed.

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

                              It worked great. Thanks!

                              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