Post by Scott McPhillips [MVP]Post by MattCHi,
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").