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. create a batch of button in code, and the BN_CLICKED messages map to one function onButtonClick()?

create a batch of button in code, and the BN_CLICKED messages map to one function onButtonClick()?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
15 Posts 3 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.
  • Z zeus_master

    Hi, would you please give a hand? how to create a batch of button(more than 200) in the code? use the array? And then mapping the messages to one function onButtonClick()? void onButtonClick(){ ....// differentiate the buttons? ....// } .. -- modified at 3:12 Wednesday 10th May, 2006

    N Offline
    N Offline
    Nibu babu thomas
    wrote on last edited by
    #2

    Store these buttons inside a CTypedPtrMap or CTypedPtrList based on your requirements. Map requires key value pairs. For key you can use the Id of the button. Override OnCommand to dynamically handle messages from these buttons instead of providing message map entries.


    Nibu thomas Software Developer Faqs by Michael dunn

    Z 1 Reply Last reply
    0
    • Z zeus_master

      Hi, would you please give a hand? how to create a batch of button(more than 200) in the code? use the array? And then mapping the messages to one function onButtonClick()? void onButtonClick(){ ....// differentiate the buttons? ....// } .. -- modified at 3:12 Wednesday 10th May, 2006

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #3

      You can create dynamically the button (I mean, not through the resource editor but by calling the Create member function). Then, you can use the ON_COMMAND_RANGE[^] macro to specify a range of ID. Don't forget to 'reserve' this range in resource.h.

      Z 1 Reply Last reply
      0
      • N Nibu babu thomas

        Store these buttons inside a CTypedPtrMap or CTypedPtrList based on your requirements. Map requires key value pairs. For key you can use the Id of the button. Override OnCommand to dynamically handle messages from these buttons instead of providing message map entries.


        Nibu thomas Software Developer Faqs by Michael dunn

        Z Offline
        Z Offline
        zeus_master
        wrote on last edited by
        #4

        thank you for your kindness. I'm afraid I don't know the way of "Store these buttons inside a CTypedPtrMap or CTypedPtrList based on your requirements. Map requires key value pairs. For key you can use the Id of the button." would you show me the simple code? and another base-question is dynamically creating button: I wrote the (a) and (b) in function OnInitDialog() (a) CButton btn; btn.Create("Hi,World",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20,80,40),this,104); (b) CButton *btn = new CButton; btn->Create("Hi,World",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20,80,40),this,104); it seems that the (a) can't work, but (b) is ok. is there any different using pointer or not?

        C 1 Reply Last reply
        0
        • Z zeus_master

          thank you for your kindness. I'm afraid I don't know the way of "Store these buttons inside a CTypedPtrMap or CTypedPtrList based on your requirements. Map requires key value pairs. For key you can use the Id of the button." would you show me the simple code? and another base-question is dynamically creating button: I wrote the (a) and (b) in function OnInitDialog() (a) CButton btn; btn.Create("Hi,World",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20,80,40),this,104); (b) CButton *btn = new CButton; btn->Create("Hi,World",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20,80,40),this,104); it seems that the (a) can't work, but (b) is ok. is there any different using pointer or not?

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #5

          zeus_master wrote:

          it seems that the (a) can't work, but (b) is ok. is there any different using pointer or not?

          Yes, in your case: you are probably declaring the CButton inside the OnInitDialog function, so its scope is limited to the function. Once the function is finished, the CButton destructor will be called, thus removing the button from the dialog. You won't have the problem if it's a member variable of the class. To store the buttons, you can also simply store them in a vector or list. Don't forget to change the ID of each button also (and be sure the range of ID's you are using is 'reserved' in the resource.h file, otherwise, some other controls could take the same id and you will be in troubles).

          Z 1 Reply Last reply
          0
          • C Cedric Moonen

            zeus_master wrote:

            it seems that the (a) can't work, but (b) is ok. is there any different using pointer or not?

            Yes, in your case: you are probably declaring the CButton inside the OnInitDialog function, so its scope is limited to the function. Once the function is finished, the CButton destructor will be called, thus removing the button from the dialog. You won't have the problem if it's a member variable of the class. To store the buttons, you can also simply store them in a vector or list. Don't forget to change the ID of each button also (and be sure the range of ID's you are using is 'reserved' in the resource.h file, otherwise, some other controls could take the same id and you will be in troubles).

            Z Offline
            Z Offline
            zeus_master
            wrote on last edited by
            #6

            thank you again for your timely teaching. I wrote it like this, but it was faild: CButton * btn[5];// = new 5 CButton int i =1; CString str; for (i=0;i<5;i++) { str.Format("%0x",i); // for set button caption btn[i]->Create(str,WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20+30*i,80,40+30*i),this,104+i); } :confused:

            C 1 Reply Last reply
            0
            • C Cedric Moonen

              You can create dynamically the button (I mean, not through the resource editor but by calling the Create member function). Then, you can use the ON_COMMAND_RANGE[^] macro to specify a range of ID. Don't forget to 'reserve' this range in resource.h.

              Z Offline
              Z Offline
              zeus_master
              wrote on last edited by
              #7

              wow, It's really a wouldful method! thany very much.:laugh:

              1 Reply Last reply
              0
              • Z zeus_master

                thank you again for your timely teaching. I wrote it like this, but it was faild: CButton * btn[5];// = new 5 CButton int i =1; CString str; for (i=0;i<5;i++) { str.Format("%0x",i); // for set button caption btn[i]->Create(str,WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20+30*i,80,40+30*i),this,104+i); } :confused:

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #8

                Try to use vector or list from the STL instead. In your class, delcare a member variable (don't forget to include vector):

                #include <vector>

                CMyClass
                {
                private:
                std::vector<CButton> m_MyButtons;
                };

                Then in OnInitDialog (or where you create the buttons):

                std::vector<CButton>:iterator iter = m_MyButtons.begin()
                for(iter; iter!=m_MyButtons.end();iter++)
                {
                (*iter).Create(str,WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20+30*i,80,40+30*i),this,104+i);
                }

                I didn't test the code so there are perhaps some errors. I strongly suggest you read some tutorials on the use of containers of the STL (list, vectors, ...). They are VERY usefull and much more safer than dynamic arrays. There is some tutorials on this website.

                Z 1 Reply Last reply
                0
                • C Cedric Moonen

                  Try to use vector or list from the STL instead. In your class, delcare a member variable (don't forget to include vector):

                  #include <vector>

                  CMyClass
                  {
                  private:
                  std::vector<CButton> m_MyButtons;
                  };

                  Then in OnInitDialog (or where you create the buttons):

                  std::vector<CButton>:iterator iter = m_MyButtons.begin()
                  for(iter; iter!=m_MyButtons.end();iter++)
                  {
                  (*iter).Create(str,WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20+30*i,80,40+30*i),this,104+i);
                  }

                  I didn't test the code so there are perhaps some errors. I strongly suggest you read some tutorials on the use of containers of the STL (list, vectors, ...). They are VERY usefull and much more safer than dynamic arrays. There is some tutorials on this website.

                  Z Offline
                  Z Offline
                  zeus_master
                  wrote on last edited by
                  #9

                  is it std::vector::iterator? how to initialize m_MyButtons? std::vector m_MyButtons(0,5);? I think my stupid project/question have made you crazy,sorry again. :((

                  C 2 Replies Last reply
                  0
                  • Z zeus_master

                    is it std::vector::iterator? how to initialize m_MyButtons? std::vector m_MyButtons(0,5);? I think my stupid project/question have made you crazy,sorry again. :((

                    C Offline
                    C Offline
                    Cedric Moonen
                    wrote on last edited by
                    #10

                    zeus_master wrote:

                    is it std::vector::iterator?

                    Yes sorry, it was a typo. (I suppose the < and > were removed in your post because you didn't check the 'Ignore HTML tags' check box).

                    zeus_master wrote:

                    how to initialize m_MyButtons?

                    You don't initialize them. The constructor of the class is called and after that, you just need to call Create for all the button (see the code I posted).

                    1 Reply Last reply
                    0
                    • Z zeus_master

                      is it std::vector::iterator? how to initialize m_MyButtons? std::vector m_MyButtons(0,5);? I think my stupid project/question have made you crazy,sorry again. :((

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #11

                      Ooops, sorry, I just realized there is something missing in the code: buttons are never added in the vector (so basically what you have now is an empty vetor, not very usefull isn't ?). So, to do that remove the code with the iterator and replace everything by this:

                      for (int i=0;i<10;i++)
                      {
                      CButton NewButton;
                      NewButton.Create(str,WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20+30*i,80,40+30*i),this,104+i);
                      m_MyButtons.push_back(NewButton);
                      }

                      So, this will create 10 buttons and add them to the vector. Then, afterwards, if you need to access the buttons again, you can use the code I previously posted (with the iterator): it is used to retrieve data from the vector. Hope this help, sorry to have forgotten that :-O

                      Z 2 Replies Last reply
                      0
                      • C Cedric Moonen

                        Ooops, sorry, I just realized there is something missing in the code: buttons are never added in the vector (so basically what you have now is an empty vetor, not very usefull isn't ?). So, to do that remove the code with the iterator and replace everything by this:

                        for (int i=0;i<10;i++)
                        {
                        CButton NewButton;
                        NewButton.Create(str,WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20+30*i,80,40+30*i),this,104+i);
                        m_MyButtons.push_back(NewButton);
                        }

                        So, this will create 10 buttons and add them to the vector. Then, afterwards, if you need to access the buttons again, you can use the code I previously posted (with the iterator): it is used to retrieve data from the vector. Hope this help, sorry to have forgotten that :-O

                        Z Offline
                        Z Offline
                        zeus_master
                        wrote on last edited by
                        #12

                        I met compiler error below: c:\program files\microsoft visual studio\vc98\include\xutility(39) : error C2582: 'CButton' : 'operator =' function is unavailable c:\program files\microsoft visual studio\vc98\include\vector(170) : see reference to function template instantiation 'void __cdecl std::fill(class CButton *,class CButton *,const class CButton &)' being compiled c:\program files\microsoft visual studio\vc98\include\xutility(25) : error C2582: 'CButton' : 'operator =' function is unavailable c:\program files\microsoft visual studio\vc98\include\vector(174) : see reference to function template instantiation 'class CButton *__cdecl std::copy_backward(class CButton *,class CButton *,class CButton *)' being compiled c:\program files\microsoft visual studio\vc98\include\xmemory(34) : error C2558: class 'CButton' : no copy constructor available c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see reference to function template instantiation 'void __cdecl std::_Construct(class CButton *,const class CButton &)' being compiled Error executing cl.exe.

                        C 1 Reply Last reply
                        0
                        • Z zeus_master

                          I met compiler error below: c:\program files\microsoft visual studio\vc98\include\xutility(39) : error C2582: 'CButton' : 'operator =' function is unavailable c:\program files\microsoft visual studio\vc98\include\vector(170) : see reference to function template instantiation 'void __cdecl std::fill(class CButton *,class CButton *,const class CButton &)' being compiled c:\program files\microsoft visual studio\vc98\include\xutility(25) : error C2582: 'CButton' : 'operator =' function is unavailable c:\program files\microsoft visual studio\vc98\include\vector(174) : see reference to function template instantiation 'class CButton *__cdecl std::copy_backward(class CButton *,class CButton *,class CButton *)' being compiled c:\program files\microsoft visual studio\vc98\include\xmemory(34) : error C2558: class 'CButton' : no copy constructor available c:\program files\microsoft visual studio\vc98\include\xmemory(66) : see reference to function template instantiation 'void __cdecl std::_Construct(class CButton *,const class CButton &)' being compiled Error executing cl.exe.

                          C Offline
                          C Offline
                          Cedric Moonen
                          wrote on last edited by
                          #13

                          Operator = ?? What are you doing with this operator ? :doh: Can you post the code ?

                          1 Reply Last reply
                          0
                          • C Cedric Moonen

                            Ooops, sorry, I just realized there is something missing in the code: buttons are never added in the vector (so basically what you have now is an empty vetor, not very usefull isn't ?). So, to do that remove the code with the iterator and replace everything by this:

                            for (int i=0;i<10;i++)
                            {
                            CButton NewButton;
                            NewButton.Create(str,WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20+30*i,80,40+30*i),this,104+i);
                            m_MyButtons.push_back(NewButton);
                            }

                            So, this will create 10 buttons and add them to the vector. Then, afterwards, if you need to access the buttons again, you can use the code I previously posted (with the iterator): it is used to retrieve data from the vector. Hope this help, sorry to have forgotten that :-O

                            Z Offline
                            Z Offline
                            zeus_master
                            wrote on last edited by
                            #14

                            I didn't do anything. I just creat a new dialog based project named stl. and according your mean,add the code in the place your said: stldlg.h // stlDlg.h : header file // #if !defined(AFX_STLDLG_H__0E3FD69C_3BD2_48E1_B6A9_3C850CD124FA__INCLUDED_) #define AFX_STLDLG_H__0E3FD69C_3BD2_48E1_B6A9_3C850CD124FA__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include ///////////////////////////////////////////////////////////////////////////// // CStlDlg dialog class CStlDlg : public CDialog { // Construction public: CStlDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CStlDlg) enum { IDD = IDD_STL_DIALOG }; // NOTE: the ClassWizard will add data members here //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CStlDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL private: std::vector m_MyButtons; // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CStlDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_STLDLG_H__0E3FD69C_3BD2_48E1_B6A9_3C850CD124FA__INCLUDED_) then add the initialization in BOOL CStlDlg::OnInitDialog() // TODO: Add extra initialization here CString str="stlbtn"; for (int i=0;i<10;i++) { CButton NewButton; NewButton.Create(str,WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20+30*i,80,40+30*i),this,104+i); m_MyButtons.push_back(NewButton); }

                            C 1 Reply Last reply
                            0
                            • Z zeus_master

                              I didn't do anything. I just creat a new dialog based project named stl. and according your mean,add the code in the place your said: stldlg.h // stlDlg.h : header file // #if !defined(AFX_STLDLG_H__0E3FD69C_3BD2_48E1_B6A9_3C850CD124FA__INCLUDED_) #define AFX_STLDLG_H__0E3FD69C_3BD2_48E1_B6A9_3C850CD124FA__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include ///////////////////////////////////////////////////////////////////////////// // CStlDlg dialog class CStlDlg : public CDialog { // Construction public: CStlDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CStlDlg) enum { IDD = IDD_STL_DIALOG }; // NOTE: the ClassWizard will add data members here //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CStlDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL private: std::vector m_MyButtons; // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CStlDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_STLDLG_H__0E3FD69C_3BD2_48E1_B6A9_3C850CD124FA__INCLUDED_) then add the initialization in BOOL CStlDlg::OnInitDialog() // TODO: Add extra initialization here CString str="stlbtn"; for (int i=0;i<10;i++) { CButton NewButton; NewButton.Create(str,WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,20+30*i,80,40+30*i),this,104+i); m_MyButtons.push_back(NewButton); }

                              C Offline
                              C Offline
                              Cedric Moonen
                              wrote on last edited by
                              #15

                              Ok, then it is because the CButton class doesn't allow 'copying' itself (needed to be stored in the vector). Then, we can go the 'old way' ;): just use a plain C array: CButton* m_pButtons; And in the constructor: m_pButtons = NULL; In the destructor:

                              if (m_pButtons)
                              {
                              delete[] m_pButtons;
                              m_pButtons = NULL;
                              }

                              In OnInitDialog:

                              m_pButtons = new CButton(10);
                              for (int i=0;i<10;i++)
                              m_pButtons[i].Create(.....);

                              Sorry for the time lost :sigh:

                              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