Button Caption
-
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.
-
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.
Assuming your controls are in a dialog box: If you are using
Win32
then you may callSendDlgItemMessage
(withWM_GETTEXT
orWM_SETTEXT
, depending on your needs). In a similar way you may useMFC
'sCWnd::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] -
Assuming your controls are in a dialog box: If you are using
Win32
then you may callSendDlgItemMessage
(withWM_GETTEXT
orWM_SETTEXT
, depending on your needs). In a similar way you may useMFC
'sCWnd::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] -
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] -
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] -
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] -
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]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()
-
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()
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] -
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] -
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.
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
-
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
-
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] -
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).
-
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).
-
Assuming your controls are in a dialog box: If you are using
Win32
then you may callSendDlgItemMessage
(withWM_GETTEXT
orWM_SETTEXT
, depending on your needs). In a similar way you may useMFC
'sCWnd::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]Who do you think you are, God? (I was worried that you missed a certain poster's rants.)
-
Who do you think you are, God? (I was worried that you missed a certain poster's rants.)
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]