Re: Can I new a CDialog, then call DoModal?
"David Wilkinson" <no-reply@effisols.com> wrote in message
news:ulPrQX0NIHA.484@TK2MSFTNGP06.phx.gbl...
GT wrote:
I have a dialog dlgMetricsClassifierNew derived from CDialog. In my code
(elsewhere) I used to just do this:
dlgMetricsClassifierNew dialog;
if (dialog.DoModal() == IDOK)
{
// do my stuff
}
I have changed the way some other parts of the code work and now after
the dialog closes with an OK press, I need to interrogate the dialog to
get a couple of strings. However, when I interrogate the dialog in the
above code, all values are all NULL, so I presume the OnOK press destroys
the dialog and loses all values? So I changed my code to use New and
Create like this:
dlgMetricsClassifierNew *dialog = new dlgMetricsClassifierNew();
dialog->Create(IDD_METRICS_CLASSIFIERNEW);
The problem comes after this when I try to call DoModal after these
lines. Is it possible to create a dialog and do DoModal in this fashion?
When you "interrogate" a dialog after a DoModal() call, you must not try
to access its controls because they are gone. The dialog must copy its
control values into member variables before closing (perhaps by using
UpdateData()).
You either use DoModal() (modal dialog) or Create() (modeless dialog), not
both; here you want a modal dialog. There is no need to use "new" here (or
almost anywhere, come to that).
Exactly what I thought, but it simply wasn't working and I was side-tracked
into believing I had to new a modeless dialog and somehow call DoModal. I've
just discovered I had a local variable with the same name as the class
variable so instead of storing the strings in my class variable, they were
going into the local variable, then rapidly out of scope. The local variable
with the same name was an intermediary temporary thing I had used during
testing and forgot to remove later. Foolish, but I'm sure we've all done
something similar!
Thanks.