Re: Getting notification for dynamically added controls
"Zapanaz" <http://joecosby.com/code/mail.pl> wrote in message
news:KMqdnb5qX8_OnSzanZ2dnUVZ_sudnZ2d@zhonka.net...
On a similar note to my last question ...
I have a situation where I will present multiple data fields to the
user which they can edit.
The number of fields can vary. I have a class which wraps there names
and values nicely.
I can build a dialog in the editor and add the fields, but that's not
really desireable. It would make more sense to just pass in the class
which wraps these fields and construct the whole thing dynamically.
My first question, really, is whether I can just create a CDialog
dynamically. I will need to create a subclass of course so I can do
things like populate the data fields and read back the user entries.
But I am really not sure how much point there is to creating it in the
editor. I mean theoretically I can just instantiate it, add the text
labels/fields (CStatic and CEdit instances) then resize the dialog to
fit them.
You should create a dialog template in the editor, even if you put no
controls on it. It has attributes such as styles and a titlebar that are
needed in order to create a dialog.
Second question. I know I should really do more research and
trial-and-error on this before I ask the question, but I am worried I
am going to get deep into it and have spent a lot of time on it and
have painted myself into a corner.
If I am creating, for example, CEdits dynamically, instead of in the
editor, they aren't going to have IDs. I guess for what I'm doing, I
am just going to need to write default values to them, then read them
when the user is done, so I don't know if I even need notifications.
But on the other hand, I often find I need to know when some things
happen, like when the user clicks in an edit field.
Without actually trying it, I don't know if this will be a problem or
not. I posted another question earlier today where I am running into
difficulty trying to get notification when a user clicks in a
dynamically-created CListCtrl subclass which doesn't have an
editor-created ID.
Every control has an ID. If you create it dynamically the ID is assigned
with your call to Create, in the last parameter. Make up some IDs for
dynamic creation and #define them in your resource.h file. Just pick a
range of numbers not used by the automatic numbering. Example:
#define ID_DYEDIT0 500
#define ID_DYEDIT1 501
etc.
Is there a general way to handle this? Is there a way to create
message map entries that don't require IDs? Or if not, is there a
workable general way of generating IDs? And in the latter case, how
do I put them in the message map?
Type in ON_CONTROL_RANGE in the message map. Give it the range of possible
IDs, such as:
ON_CONTROL_RANGE(EN_CHANGE , ID_DYEDIT0, ID_DYEDIT9, OnDyEditChange)
The handler function will be called if any of edit controls produces the
EN_CHANGE notification, and is passed the ID of the control that did it:
void Cxx::OnDyEditChange( UINT nID )
{
}
--
Scott McPhillips [VC++ MVP]