logo       

Re: Calling a WINFORM from another WINFORM: msg#00406

windows.devel.dotnet.winforms

Subject: Re: Calling a WINFORM from another WINFORM

Forms are never really in control of the application in Windows Forms
except for the brief durations in which their event handers run. The
fact is that you can have as many forms open simultaneously in one
application as your machine can cope with. None of them is in control
as such. It's the Application class that is in control of what gets to
run. However, only one window at the time can have the focus, so that
might be all you require.

It's usually as simple as this:

... in the login window code...
private void OnLoginClick(object sender, EventArgs e)
{
if (CredentialsValid())
{
this.Close();
NextForm f = new NextForm();
f.Show();
}
}

That will close the existing form and then open a new one.

The one catch here is that you need to make sure that the Application
class keeps running. By default, the Windows Forms application wizard
will create a project that initializes the Application class in such a
way that it will assume that the first window your app shows is also the
'main' window. So you typically see something like this:

static void Main()
{
Form1 f = new Form1();
Application.Run(f);
}

or code to that effect. If you chose to make Form1 the login dialog,
you'll have a problem here - when you close the login form, the
Applciation class will notice that your 'main' window has closed, and
will therefore quit. (So your NextForm might never even get to appear -
the application might quit before it manages to become visible.)

You'll need to change the startup. The simplest thing would be this:

static void Main()
{
LoginForm f = new LoginForm();
f.Show();
Application.Run();
}

Now you'll be able to close the login form without the app quitting,
leaving the new form it opens on screen. Only problem is, when you
close that, the app will remain running. Once you remove this concept
of a "Main window" the Application class runs until you explicitly tell
it to stop by calling Application.Exit(). So you'll need to make sure
you call that when you want to shut down.


Another approach is to use an ApplicationContext. This is an object
that manages the lifetime of your application. If you want you can give
it a main form, so it'll exit when that form closes, but the nice thing
is that you can change which form is the 'main' one at any given moment.
E.g.:

class App
{
public static ApplicationContext ctx;
static void Main()
{
LoginForm f = new LoginForm();
f.Show();
ctx = new ApplicationContext (f);
Application.Run(ctx);
}
}

this initially sets the LoginForm as the 'main' form, so if the user
closes the form without logging in, the application will exit
automatically. But in the login form we can do this:

NextForm f = new NextForm();
App.ctx.MainForm = f;
this.Close();
f.Show();

here we tell the application context that we would like to change which
form is the main form. We do this before closing the login window
(otherwise the app would exit). Now the application will run for as
long as the NextForm form is open.

Hope that helps.


--
Ian Griffiths - DevelopMentor
http://www.interact-sw.co.uk/iangblog/

> -----Original Message-----
> From: Darren Swartzendruber
>
> I've been coding in C# for about one year now, but only have coded
> ASP.NET, web services, single-form WIN apps and Compact Framework
apps.
> But I have never needed to worry about multiple WINFORMs being used by
a
> WIN app.
>
> So how does one transfer control of the WIN application to another
> WINFORM?
>
> For example, I have a WINFORM (Login window) that will authenticate a
> user (using webservices) and if the user is authenticated, I want to
> close the login WINFORM and call the WINFORM that handles the app.



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise