On Thu, 18 Sep 2008 12:59:01 -0700, Ethan Strauss
<Et**********@discussions.microsoft.comwrote:
I am (still) relatively new to Windows applications, most of my
experience
has been Web based, and I am confused about what exactly happens when
the
Main() method is called and how to manipulate forms opening & closing.
I recommend looking at the Main() method then. It's in, by default, the
Program.cs file. Also by default, it only has three things it does: two
calls to initialize some of the GUI stuff, and then a statement that calls
Application.Run() with a new form instance. Application.Run() will return
once the form passed to it is closed, closing all other open forms owned
by that thread (which is usually all other open forms without exception)
at the same time.
One way to change the behavior of your program is to change the Main()
method so that it calls Application.Run() differently. For example, if
you don't pass it a form instance, then closing any form won't cause
Application.Run() to exit. Only a call to Application.ExitThread()
would. That gives you more control over the lifetime of your application,
regardless of which forms are shown or closed.
An example of this issue is as follows. I have a logon form open as the
first thing. The main functional form opens when a user has successfully
logged on. From the main form, a user should be able to logout which
will
reshow the logon form and the state should be essentially identical to if
they have never logged on in the first place. What I find is that I can't
close any form in this string, or everything shuts down, so I have to
hide
forms. But if I do that, then they still lurk in the background and do
strange things.
"Do strange things"? Like what? If you forms are doing strange things
when hidden, then that's something you should be concerned about. Not
because it's an impediment to the basic functionality, but because you've
made a mistake in authoring your forms somehow. Ideally, your forms
should be strictly UI, but in any case there's no reason that they should
do anything "strange" while hidden.
Also, it should not be trued that you "can't close any form in this
string". By default, there's only one form that's critical: the one
passed to Application.Run(). You can show and close arbitrarily many
other forms without problem; it's only closing that first form that would
cause the application to end.
So, another option is to show/close other forms as you'd like, just taking
care to never close the logon form. Hide that when it's not needed, show
it when it is. Close it when you want the application to exit. If it
does "strange things" when it's hidden, fix it so that it doesn't.
I have been thinking that I should make an orchestrator class with a
method
something like this
public void ReplaceCurrentlyShownForm(Form toOpen, Form
currentlyOpen)
{
currentlyOpen.Close();
currentlyOpen.Dispose();
toOpen.Show();
}
and have Main instantiate this class which then runs until the whole app
is
closed down.
Would that make sense?
How would that help? At some point, you still need to call
Application.Run(). And with or without a method such as above, the same
rules apply.
By the way, Form.Close() does a Dispose() implicitly, unless you used
Form.ShowDialog() to show the form. There's no need to call Dispose()
after closing a non-modal form.
Pete