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. Button Caption

Button Caption

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
16 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.
  • S shiv nand

    i have created a 5 Buttons dynamically. eg : 2) Button 1 contol id :5000 1) Button 2 contol id :5001 1) Button 3 contol id :5002 1) Button 4 contol id :5003 1) Button 5 contol id :5003, i need to select caption name "Button 1" using its control id 5000; i used RButtonup and i got control id. But i dont know how to get the caption of Button .Please help.

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #2

    Assuming your controls are in a dialog box: If you are using Win32 then you may call SendDlgItemMessage (with WM_GETTEXT or WM_SETTEXT, depending on your needs). In a similar way you may use MFC's CWnd::SendDlgItemMessage. :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    S J 2 Replies Last reply
    0
    • C CPallini

      Assuming your controls are in a dialog box: If you are using Win32 then you may call SendDlgItemMessage (with WM_GETTEXT or WM_SETTEXT, depending on your needs). In a similar way you may use MFC's CWnd::SendDlgItemMessage. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      S Offline
      S Offline
      shiv nand
      wrote on last edited by
      #3

      CWnd::SendDlgItemMessage, i have no idia if you give example, it is very helpful to me.

      C 1 Reply Last reply
      0
      • S shiv nand

        CWnd::SendDlgItemMessage, i have no idia if you give example, it is very helpful to me.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #4

        Inside a method of your window class (the parent window of your buttons) call, for instance:

        SendDlgItemMessage(5000, WM_SETTEXT, 0 , (LPARAM) _T("BUTTON 0"));

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        S 1 Reply Last reply
        0
        • C CPallini

          Inside a method of your window class (the parent window of your buttons) call, for instance:

          SendDlgItemMessage(5000, WM_SETTEXT, 0 , (LPARAM) _T("BUTTON 0"));

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          S Offline
          S Offline
          shiv nand
          wrote on last edited by
          #5

          no i am not getting, please is there any sample code.

          C 1 Reply Last reply
          0
          • S shiv nand

            no i am not getting, please is there any sample code.

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #6

            If you post your (relevant) code may be I can tell you how to modify it. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            S 1 Reply Last reply
            0
            • C CPallini

              If you post your (relevant) code may be I can tell you how to modify it. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              S Offline
              S Offline
              shiv nand
              wrote on last edited by
              #7

              ON_COMMAND_RANGE(5000,5100,OnButtonRange) void CTest::OnShowWindow(BOOL bShow, UINT nStatus) { CDialog::OnShowWindow(bShow, nStatus); CButton* m_btDynamic; CString csText; int k=0; for(int i=0;i<5;i++) { m_btDynamic = new CButton(); csText.Format(L"Button %d",i+1); m_btDynamic->Create(csText, BS_FLAT|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON|BS_ICON, CRect(10+k ,10+k,60+k,60+k),this, 5000+i); k=k+30; } } void CTest::OnButtonRange(UINT nIDforControl) { int iSelectedControlID; iSelectedControlID = nIDforControl; // here i want button caption } Where i should use SendDlgItemMessage()

              C 1 Reply Last reply
              0
              • S shiv nand

                ON_COMMAND_RANGE(5000,5100,OnButtonRange) void CTest::OnShowWindow(BOOL bShow, UINT nStatus) { CDialog::OnShowWindow(bShow, nStatus); CButton* m_btDynamic; CString csText; int k=0; for(int i=0;i<5;i++) { m_btDynamic = new CButton(); csText.Format(L"Button %d",i+1); m_btDynamic->Create(csText, BS_FLAT|WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON|BS_ICON, CRect(10+k ,10+k,60+k,60+k),this, 5000+i); k=k+30; } } void CTest::OnButtonRange(UINT nIDforControl) { int iSelectedControlID; iSelectedControlID = nIDforControl; // here i want button caption } Where i should use SendDlgItemMessage()

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #8

                Well, you already set the caption for you button with known values, so what's your need now? BTW I suggest you to maintain the CButton pointers in a member variable: in class declaration:

                CButton * m_btDynamic[5];

                then, in you function you may do

                void CTest::OnShowWindow(BOOL bShow, UINT nStatus)
                {
                CDialog::OnShowWindow(bShow, nStatus);

                 CString csText;    
                 int   k=0;
                     
                 for(int i=0;i<5;i++)
                 {
                   m\_btDynamic\[i\] = new CButton();
                          
                   csText.Format(L"Button %d",i+1);         
                   m\_btDynamic\[i\]->Create(csText,BS\_FLAT|WS\_VISIBLE|WS\_CHILD|BS\_PUSHBUTTON|BS\_ICON,
                       CRect(10+k ,10+k,60+k,60+k),this, 5000+i);
                    k=k+30;
                 }
                

                }

                so that whenever you need to access the caption of, say, third button, you may call

                m_btDynamic[2]->GetWindowText();

                :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                S 1 Reply Last reply
                0
                • C CPallini

                  Well, you already set the caption for you button with known values, so what's your need now? BTW I suggest you to maintain the CButton pointers in a member variable: in class declaration:

                  CButton * m_btDynamic[5];

                  then, in you function you may do

                  void CTest::OnShowWindow(BOOL bShow, UINT nStatus)
                  {
                  CDialog::OnShowWindow(bShow, nStatus);

                   CString csText;    
                   int   k=0;
                       
                   for(int i=0;i<5;i++)
                   {
                     m\_btDynamic\[i\] = new CButton();
                            
                     csText.Format(L"Button %d",i+1);         
                     m\_btDynamic\[i\]->Create(csText,BS\_FLAT|WS\_VISIBLE|WS\_CHILD|BS\_PUSHBUTTON|BS\_ICON,
                         CRect(10+k ,10+k,60+k,60+k),this, 5000+i);
                      k=k+30;
                   }
                  

                  }

                  so that whenever you need to access the caption of, say, third button, you may call

                  m_btDynamic[2]->GetWindowText();

                  :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  S Offline
                  S Offline
                  shiv nand
                  wrote on last edited by
                  #9

                  thanks CPallini, this is not exact code i have posted. in my actual code i have created 100 buttons, same way as u told. but the problem is if user right click on any button i want that caption.

                  C 1 Reply Last reply
                  0
                  • S shiv nand

                    thanks CPallini, this is not exact code i have posted. in my actual code i have created 100 buttons, same way as u told. but the problem is if user right click on any button i want that caption.

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #10

                    You may do this way (in your event handler):

                    CButton * pClickedButton = (CButton *) GetDlgItem( id ); // the id is known, as you stated
                    CString sClickedCaption = pClickedButton->GetWindowText();

                    :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    modified on Friday, March 26, 2010 10:26 AM

                    S 1 Reply Last reply
                    0
                    • C CPallini

                      You may do this way (in your event handler):

                      CButton * pClickedButton = (CButton *) GetDlgItem( id ); // the id is known, as you stated
                      CString sClickedCaption = pClickedButton->GetWindowText();

                      :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      modified on Friday, March 26, 2010 10:26 AM

                      S Offline
                      S Offline
                      shiv nand
                      wrote on last edited by
                      #11

                      i am getting error error C2039: 'GetText' : is not a member of 'CButton'

                      I C 2 Replies Last reply
                      0
                      • S shiv nand

                        i am getting error error C2039: 'GetText' : is not a member of 'CButton'

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #12

                        Sorry, I should have fixed it now. :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        1 Reply Last reply
                        0
                        • S shiv nand

                          i am getting error error C2039: 'GetText' : is not a member of 'CButton'

                          I Offline
                          I Offline
                          Iain Clarke Warrior Programmer
                          wrote on last edited by
                          #13

                          It's GetWindowText, not GetText. But if you look at the documentation for CWnd on msdn, you can see the details for yourself too. Good luck! Iain.

                          I have now moved to Sweden for love (awwww).

                          S 1 Reply Last reply
                          0
                          • I Iain Clarke Warrior Programmer

                            It's GetWindowText, not GetText. But if you look at the documentation for CWnd on msdn, you can see the details for yourself too. Good luck! Iain.

                            I have now moved to Sweden for love (awwww).

                            S Offline
                            S Offline
                            shiv nand
                            wrote on last edited by
                            #14

                            ok thank you i will try

                            1 Reply Last reply
                            0
                            • C CPallini

                              Assuming your controls are in a dialog box: If you are using Win32 then you may call SendDlgItemMessage (with WM_GETTEXT or WM_SETTEXT, depending on your needs). In a similar way you may use MFC's CWnd::SendDlgItemMessage. :)

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              J Offline
                              J Offline
                              Joe Woodbury
                              wrote on last edited by
                              #15

                              Who do you think you are, God? (I was worried that you missed a certain poster's rants.)

                              C 1 Reply Last reply
                              0
                              • J Joe Woodbury

                                Who do you think you are, God? (I was worried that you missed a certain poster's rants.)

                                C Offline
                                C Offline
                                CPallini
                                wrote on last edited by
                                #16

                                To your knees! (to hear the softly spoken magic spells) :-\

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                [My articles]

                                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