Discussion:
How to update checkbox?
(too old to reply)
MattC
2004-01-04 04:24:27 UTC
Permalink
Hi,

I am new to MFC.

I have the ID for a checkbox, say IDC_CHECK1. Using CWnd*
pItem = GetDlgItem (IDC_CHECK1), I can get a CWnd* pointer.
But I don't know how to cast this pointer to a checkbox
control object. I simply want to set/change the status of
the checkbox.

thanks in advance!

MattC
Scott McPhillips [MVP]
2004-01-04 04:39:36 UTC
Permalink
Post by MattC
Hi,
I am new to MFC.
I have the ID for a checkbox, say IDC_CHECK1. Using CWnd*
pItem = GetDlgItem (IDC_CHECK1), I can get a CWnd* pointer.
But I don't know how to cast this pointer to a checkbox
control object. I simply want to set/change the status of
the checkbox.
thanks in advance!
MattC
You can do it by forcing a cast (CButton*)GetDlgItem() but why would you
want to do that?

You should use the IDE/Wizard to add a member variable of type CButton
to the parent window. Then you can call this variable's SetCheck,
GetCheck, etc. as well as all the inherited CWnd functions like
EnableWindow.
--
Scott McPhillips [VC++ MVP]
MattC
2004-01-04 05:02:52 UTC
Permalink
Jeff and Scott,

Thank you both for the reply. I also got it to work with:
CWnd::UpdateData method.

-MattC
-----Original Message-----
Post by MattC
Hi,
I am new to MFC.
I have the ID for a checkbox, say IDC_CHECK1. Using
CWnd*
Post by MattC
pItem = GetDlgItem (IDC_CHECK1), I can get a CWnd*
pointer.
Post by MattC
But I don't know how to cast this pointer to a checkbox
control object. I simply want to set/change the status
of
Post by MattC
the checkbox.
thanks in advance!
MattC
You can do it by forcing a cast (CButton*)GetDlgItem()
but why would you
want to do that?
You should use the IDE/Wizard to add a member variable of
type CButton
to the parent window. Then you can call this variable's
SetCheck,
GetCheck, etc. as well as all the inherited CWnd
functions like
EnableWindow.
--
Scott McPhillips [VC++ MVP]
.
David A. Mair
2004-01-04 19:54:16 UTC
Permalink
Post by Scott McPhillips [MVP]
Post by MattC
Hi,
I am new to MFC.
I have the ID for a checkbox, say IDC_CHECK1. Using CWnd*
pItem = GetDlgItem (IDC_CHECK1), I can get a CWnd* pointer.
But I don't know how to cast this pointer to a checkbox
control object. I simply want to set/change the status of
the checkbox.
thanks in advance!
MattC
You can do it by forcing a cast (CButton*)GetDlgItem() but why would you
want to do that?
You should use the IDE/Wizard to add a member variable of type CButton
to the parent window. Then you can call this variable's SetCheck,
GetCheck, etc. as well as all the inherited CWnd functions like
EnableWindow.
There are some times when it is preferrable to use GetDlgItem(). For
example, one can imagine cases where when handling control change
notifications where the change precipitates the need to change the state of
some other control. I have a dialog in a SDI app, for example, where when I
check one checkbox I enable a bunch of related controls and set some of them
to default or saved values. That sometimes involves checking a checkbox
after the first has been checked. Specifically, it's a webcam app and there
is a checkbox to enable frame and time-lapse capture. For each of these
there is a set of controls that only need to be enabled (and have a
particular) value if the enable functionality checkbox is checked. It's not
appropriate for me to use UpdateData() in that case and, besides, I'd have
to save and validate, modify some of the members and then update the
controls in order for it to be safe:

UpdateData(TRUE);
m_CheckBox = TRUE;
.
.
.
UpdateData(FALSE);

I'm getting to the point where while I still use DDX, I find myself doing
more and more GetDlgItem/GetDlgItemText/SetDlgItemText just because it is
more convenient in context.

It's probably safe for the OP's case to use DDX and it is probably true that
DDX is appropriate for the majority of cases but I think it is not the case
that there is no reason to not use DDX (as implied by "why would you want to
do that").
Jeff Partch [MVP]
2004-01-04 21:40:36 UTC
Permalink
Post by David A. Mair
Post by Scott McPhillips [MVP]
Post by MattC
Hi,
I am new to MFC.
I have the ID for a checkbox, say IDC_CHECK1. Using CWnd*
pItem = GetDlgItem (IDC_CHECK1), I can get a CWnd* pointer.
But I don't know how to cast this pointer to a checkbox
control object. I simply want to set/change the status of
the checkbox.
thanks in advance!
MattC
You can do it by forcing a cast (CButton*)GetDlgItem() but why would you
want to do that?
You should use the IDE/Wizard to add a member variable of type CButton
to the parent window. Then you can call this variable's SetCheck,
GetCheck, etc. as well as all the inherited CWnd functions like
EnableWindow.
There are some times when it is preferrable to use GetDlgItem(). For
example, one can imagine cases where when handling control change
notifications where the change precipitates the need to change the state of
some other control. I have a dialog in a SDI app, for example, where when I
check one checkbox I enable a bunch of related controls and set some of them
to default or saved values. That sometimes involves checking a checkbox
after the first has been checked. Specifically, it's a webcam app and there
is a checkbox to enable frame and time-lapse capture. For each of these
there is a set of controls that only need to be enabled (and have a
particular) value if the enable functionality checkbox is checked. It's not
appropriate for me to use UpdateData() in that case and, besides, I'd have
to save and validate, modify some of the members and then update the
UpdateData(TRUE);
m_CheckBox = TRUE;
.
.
.
UpdateData(FALSE);
I'm getting to the point where while I still use DDX, I find myself doing
more and more GetDlgItem/GetDlgItemText/SetDlgItemText just because it is
more convenient in context.
It's probably safe for the OP's case to use DDX and it is probably true that
DDX is appropriate for the majority of cases but I think it is not the case
that there is no reason to not use DDX (as implied by "why would you want to
do that").
While that may argue against UpdateData, what does all that have to do with
Scott's recommending the use of control member variables instead of
GetDlgItem?
--
Jeff Partch [VC++ MVP]
David A. Mair
2004-01-04 21:47:15 UTC
Permalink
Post by Jeff Partch [MVP]
Post by David A. Mair
Post by Scott McPhillips [MVP]
Post by MattC
Hi,
I am new to MFC.
I have the ID for a checkbox, say IDC_CHECK1. Using CWnd*
pItem = GetDlgItem (IDC_CHECK1), I can get a CWnd* pointer.
But I don't know how to cast this pointer to a checkbox
control object. I simply want to set/change the status of
the checkbox.
thanks in advance!
MattC
You can do it by forcing a cast (CButton*)GetDlgItem() but why would you
want to do that?
You should use the IDE/Wizard to add a member variable of type CButton
to the parent window. Then you can call this variable's SetCheck,
GetCheck, etc. as well as all the inherited CWnd functions like
EnableWindow.
There are some times when it is preferrable to use GetDlgItem(). For
example, one can imagine cases where when handling control change
notifications where the change precipitates the need to change the state
of
Post by David A. Mair
some other control. I have a dialog in a SDI app, for example, where
when
Post by Jeff Partch [MVP]
I
Post by David A. Mair
check one checkbox I enable a bunch of related controls and set some of
them
Post by David A. Mair
to default or saved values. That sometimes involves checking a checkbox
after the first has been checked. Specifically, it's a webcam app and
there
Post by David A. Mair
is a checkbox to enable frame and time-lapse capture. For each of these
there is a set of controls that only need to be enabled (and have a
particular) value if the enable functionality checkbox is checked. It's
not
Post by David A. Mair
appropriate for me to use UpdateData() in that case and, besides, I'd have
to save and validate, modify some of the members and then update the
UpdateData(TRUE);
m_CheckBox = TRUE;
.
.
.
UpdateData(FALSE);
I'm getting to the point where while I still use DDX, I find myself doing
more and more GetDlgItem/GetDlgItemText/SetDlgItemText just because it is
more convenient in context.
It's probably safe for the OP's case to use DDX and it is probably true
that
Post by David A. Mair
DDX is appropriate for the majority of cases but I think it is not the
case
Post by David A. Mair
that there is no reason to not use DDX (as implied by "why would you
want
Post by Jeff Partch [MVP]
to
Post by David A. Mair
do that").
While that may argue against UpdateData, what does all that have to do with
Scott's recommending the use of control member variables instead of
GetDlgItem?
Scott's statement was bare: "You can do it by forcing a cast
(CButton*)GetDlgItem() but why would you want to do that?". Someone could
infer from this that there is no reason to do it any other way than DDX in
any circumstances. Scott probably never intended that but it was worth
making the point that DDX is often the optimal method but there are cases
for which it is not. Without more detail from the OP we can only assume
that his case is appropriate for DDX (very likely) but it may not have been.
Jeff Partch [MVP]
2004-01-04 22:31:33 UTC
Permalink
Post by David A. Mair
Post by Jeff Partch [MVP]
While that may argue against UpdateData, what does all that have to do
with
Post by Jeff Partch [MVP]
Scott's recommending the use of control member variables instead of
GetDlgItem?
Scott's statement was bare: "You can do it by forcing a cast
(CButton*)GetDlgItem() but why would you want to do that?". Someone could
infer from this that there is no reason to do it any other way than DDX in
any circumstances. Scott probably never intended that but it was worth
making the point that DDX is often the optimal method but there are cases
for which it is not. Without more detail from the OP we can only assume
that his case is appropriate for DDX (very likely) but it may not have been.
Hmm. As I read it, Scott's statement is, "You can do it by forcing a cast
(CButton*)GetDlgItem() but why would you want to do that? You should use the
IDE/Wizard to add a member variable of type CButton to the parent window".
While MFC does use DDX_Control to subclass the control member variable, I
fail to see how what Scott said was bare or misleading or open to the
misinterpretation you've found in it.
--
Jeff Partch [VC++ MVP]
Scott McPhillips [MVP]
2004-01-04 22:29:44 UTC
Permalink
Post by David A. Mair
Scott's statement was bare: "You can do it by forcing a cast
(CButton*)GetDlgItem() but why would you want to do that?". Someone could
infer from this that there is no reason to do it any other way than DDX in
any circumstances. Scott probably never intended that but it was worth
making the point that DDX is often the optimal method but there are cases
for which it is not. Without more detail from the OP we can only assume
that his case is appropriate for DDX (very likely) but it may not have been.
Hey David!

The reason I wrote "Why would you want to do that?" is that I think it
is obvious that

m_mycheck.SetCheck(1);

is better code than:

((CButton*)GetDlgItem(ID_MYCHECK))->SetCheck(1);

in absolutely all circumstances.

You seem to be contrasting GetDlgItem vs. UpdateData and pointing out
(correctly) why UpdateData can be awkward. But what I suggested is
neither of these!
--
Scott McPhillips [VC++ MVP]
David A. Mair
2004-01-04 22:40:37 UTC
Permalink
Post by Scott McPhillips [MVP]
Post by David A. Mair
Scott's statement was bare: "You can do it by forcing a cast
(CButton*)GetDlgItem() but why would you want to do that?". Someone could
infer from this that there is no reason to do it any other way than DDX in
any circumstances. Scott probably never intended that but it was worth
making the point that DDX is often the optimal method but there are cases
for which it is not. Without more detail from the OP we can only assume
that his case is appropriate for DDX (very likely) but it may not have been.
Hey David!
The reason I wrote "Why would you want to do that?" is that I think it
is obvious that
m_mycheck.SetCheck(1);
((CButton*)GetDlgItem(ID_MYCHECK))->SetCheck(1);
in absolutely all circumstances.
You seem to be contrasting GetDlgItem vs. UpdateData and pointing out
(correctly) why UpdateData can be awkward. But what I suggested is
neither of these!
Scott,

I owe you an apology for one thing, I read your message as the use of a DDX
variable, not a control variable. Sorry.

D.
David A. Mair
2004-01-04 22:51:08 UTC
Permalink
Post by Scott McPhillips [MVP]
Post by David A. Mair
Scott's statement was bare: "You can do it by forcing a cast
(CButton*)GetDlgItem() but why would you want to do that?". Someone could
infer from this that there is no reason to do it any other way than DDX in
any circumstances. Scott probably never intended that but it was worth
making the point that DDX is often the optimal method but there are cases
for which it is not. Without more detail from the OP we can only assume
that his case is appropriate for DDX (very likely) but it may not have been.
Hey David!
The reason I wrote "Why would you want to do that?" is that I think it
is obvious that
m_mycheck.SetCheck(1);
((CButton*)GetDlgItem(ID_MYCHECK))->SetCheck(1);
in absolutely all circumstances.
You seem to be contrasting GetDlgItem vs. UpdateData and pointing out
(correctly) why UpdateData can be awkward. But what I suggested is
neither of these!
I suppose that I should add that I don't agree that the control variable
case is better in absolutely all cases than GetDlgItem(), even though it
syntactically always tidier. I personally find control variables quite
messy when large numbers of them are required (particularly the way VS
modifies class declarations over time although that can obviously be hand
corrected). While the C style cast shown is legal, is a C++
dynamic_cast<>() cast not more appropriate (making the GetDlgItem() case
look even worse than it already does IMO). Plus, I like to validate that
GetDlgItem returns a non-NULL value whether or not there is no chance of it
not doing so (making it look worse still). FWIW, I use GetDlgItem()
sometimes in preference to control variables but mostly in cases where I
have large numbers of controls on a dialog and need to manipulate them in
groups. However, it's almost always for cases when I want to do things like
EnableWindow()/DisableWindow().

Jeff Partch [MVP]
2004-01-04 04:43:11 UTC
Permalink
Post by MattC
Hi,
I am new to MFC.
I have the ID for a checkbox, say IDC_CHECK1. Using CWnd*
pItem = GetDlgItem (IDC_CHECK1), I can get a CWnd* pointer.
But I don't know how to cast this pointer to a checkbox
control object. I simply want to set/change the status of
the checkbox.
Here's one way...

CButton* pCheckBox = (CButton*)GetDlgItem(IDC_CHECK1);
if (pCheckBox)
pCheckBox->SetCheck(1);
--
Jeff Partch [VC++ MVP]
Continue reading on narkive:
Loading...