Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, I have more than one forms in my program, and I would like to for example, in form1, I will call form2 and in form2 it will call form3. But the problem is when I call form2.Show, it does show the form2, but how can I unload form1? If I put form1.close, it will close everything. :( In VB, I only have to put call form2.show call unload(form1) I know that this this is because by default when I create a new WinForm application in C#, the main method consists of one line of code. That line of code is Application.Run(new Form1());. The effect of that is that the Application adds an event handler for the Closed event of the form. In that event handler it calls ExitThread. So when you close Form1 the application exits. Can I prevent this? Can I just put this line Application.Run(new Form1()); into somewhere else? I'm new in C#, so could someone help me? Cheers! Claudi
Post Follow-up to this messageI've never done this myself, so bear with me. I'm just going to think
while I type, if I may.
The problem is that the thread that is running your application, the
one you created in your main program with Application.Run(new Form1()),
is married to the form itself. When the form closes, the thread
terminates and the program is finished.
Perhaps what you need is to run something else in that thread,
something else that has a more sophisticated concept of when the
application is finished. That thing, in turn, would instantiate and
show the form. It would have to have some concept of when the program
was finished, as well (since it can't tell by simply watching for Form1
to close).
For example, you could write:
private void ShowForms()
{
Form1 f1 = new Form1();
f1.Show();
.. wait for the condition that will end the program ...
}
This could get tricky, though, because you have to write code that
blocks and waits for other threads and stuff. Too difficult for me. :)
Another method might be _not_ to close Form1. Just because you spawned
Form2, doesn't mean that Form1 has to _close_. You could just .Hide()
it so the user couldn't see it, but leave it "alive" until the user
wants to stop the application. Then close it. Then you wouldn't need
any special code at all. The only di
vantage here is that Form1 is
still using up memory... but from the user's point of view it looks the
same as the fancier solution.
I'd do it the second way myself, but then I prefer to avoid
multithreading wherever I can. :)
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.