Radio Button's Satus??
-
hello guys...im new to MFC using VS 2008. How can I get the radio buttons status in MFC using VS 2008??:confused:
-
BOOL bSelected = (CButton*) GetDlgItem(IDC_XYZ))->GetCheck();
-- "Programming is an art that fights back!"
GetCheck
returns anint
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.
-
GetCheck
returns anint
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.
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
-
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
-
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.
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
-
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
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 thoughGetDlgItem
doesn't know that) it would seem the sensible thing to do, especially if you then want to call some method on theCButton
object.It's time for a new signature.
-
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 thoughGetDlgItem
doesn't know that) it would seem the sensible thing to do, especially if you then want to call some method on theCButton
object.It's time for a new signature.
-
hello guys...im new to MFC using VS 2008. How can I get the radio buttons status in MFC using VS 2008??:confused:
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
-
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
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 fromCWnd
. That is why the upcast is required.It's time for a new signature.
-
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 fromCWnd
. That is why the upcast is required.It's time for a new signature.
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
-
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
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 toGetDlgItem()
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. RichardIt's time for a new signature.
modified on Tuesday, July 20, 2010 1:28 PM
-
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 toGetDlgItem()
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. RichardIt's time for a new signature.
modified on Tuesday, July 20, 2010 1:28 PM