How do I mimic a dialog box

D

Dom

I want a window that acts like a dialog box, but is not a dialog box.
Here is the problem:

The constructor of the form takes a great deal of time, since it needs
to read a large table in a DB. Therefore, I don't want to go through
the usual ShowDialog(), Hide(), Dispose() routines, since this will
force me to destroy the form, and construct a new one each time it is
used. Even if I skip the Dispose(), ShowDialog() seems to reset
things for me, which I also don't want. I want the user to see the
form just as it was when he was finished with it the last time it was
used. So instead, I do this:

1. Construct the form at startup.
2. Make the form invisible.
3. Set the owner of the form to the Main window. This prevents the
user from going back to the main form, while the subform is showing.
4. Make the form visible, when the user needs is.
5. Make the form invisble, when the user is through with it.

This does exactly what I need, except it seems to mimic a Show()
method, not a ShowDialog() method, in that the execution of the main
form does not stop until the user is through with the subform.

Am I missing something?
 
A

Alf P. Steinbach

I want a window that acts like a dialog box, but is not a dialog box.
Here is the problem:

The constructor of the form takes a great deal of time, since it needs
to read a large table in a DB. Therefore, I don't want to go through
the usual ShowDialog(), Hide(), Dispose() routines, since this will
force me to destroy the form, and construct a new one each time it is
used. Even if I skip the Dispose(), ShowDialog() seems to reset
things for me, which I also don't want. I want the user to see the
form just as it was when he was finished with it the last time it was
used. So instead, I do this:

1. Construct the form at startup.
2. Make the form invisible.
3. Set the owner of the form to the Main window. This prevents the
user from going back to the main form, while the subform is showing.
4. Make the form visible, when the user needs is.
5. Make the form invisble, when the user is through with it.

This does exactly what I need, except it seems to mimic a Show()
method, not a ShowDialog() method, in that the execution of the main
form does not stop until the user is through with the subform.

Am I missing something?

It seems that you're missing the idea of separating processing logic
from GUI.

Cheers & hth.,

- Alf
 
D

Dom

As Alf obliquely hints at, the biggest problem here appears to be that
you are tying your UI logic (the dialog box) to the underlying data
logic (the table).

You don't say how you're reading the large table, but however you are
accomplishing it, that logic should be trivial to execute outside the
context of the dialog form itself.  You can preserve the table itself,
without any need to preserve the form used for the dialog.

As far as seeing the form "just as it was" the previous time it was
used, you'll need to store the relevant state from the dialog when it's
closed, and restore that state when the dialog is shown again.  But
there's no requirement that the form itself stay resident; in fact, if
you are saving/restoring state, then you even have the option of
preserving the UI state between different invocations of the application.

Finally, I'll point out that in fact, when you call ShowDialog() on a
form, it already does pretty much what you want.  Since you didn't post
any code example, I can't comment on why your own attempt to do so
failed.  But the two main differences between Show() and ShowDialog() are:

   • User input is restricted to the dialog form while it's visible
("execution" of other forms is not "stopped", but those other forms
can't receive user input such as from the keyboard or mouse).

   • The form object is _not_ disposed of when the form is closed.  It's
simply hidden.

Note the second point in particular: for a form displayed with Show(),
the form object and all its children are disposed of when the form is
closed.  But for a form displayed with ShowDialog(), the form object and
its children are not disposed of.

This means that for any form object you use ShowDialog() with, you
should call Dispose() on the object before you lost track of the
reference to the object.  Usually, this would mean you need to wrap the
use of the object with a "using" statement, but in your own case it
means you should be able to just keep the reference to the form object
and reuse it the next time you want to show the dialog.

I'm not saying that's the best design; but it should work, not counting
other programming errors you might make.

Pete- Hide quoted text -

- Show quoted text -

Thanks to both of you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top