Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

unload a form (close a form)
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


Report this thread to moderator Post Follow-up to this message
Old Post
cdolphin88@yahoo.co.uk
05-18-05 09:00 AM


Re: unload a form (close a form)
I'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 divantage 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. :)


Report this thread to moderator Post Follow-up to this message
Old Post
Bruce Wood
05-18-05 09:00 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C# archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 10:16 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.