Windows Develop Bookmark and Share   
 index > Windows Forms General > Switchboard form and child form.
 

Switchboard form and child form.

Hi all:

Windows forms, .Net 2.0 C# project.

I have a simple switchboard form that loads at startup. The user can choose where they'd like to go by clicking buttons that, in turn, launch other forms.

Once the user has selected their destination I'd like the switchboard to close, but since I declare the destination form inside the switcboard that's also closing the destination form.

How can I decouple those forms so that the destination persists after the switchboard closes? I know, this is a real noob question. :-(

Thanks.

Kurt

Kurt Wimberger  Wednesday, November 19, 2008 6:07 PM

Typically, in the Program.cs file, there is a line like the following:

Code Snippet
Application
.Run(new SwitchboardForm());

Whenever the form identified in this line closes, the application exits.

As a result of the Application.Run, this takes some cleverness to work around. The easiest thing to do would be to display your switchboard as a modal form prior to Application.Run. (Even though it is modal, it will not stop the user from switching to other applications. In other words, the user won't even notice that it is modal.) Here I show how this might work where the Form to be run is returned through a custom property of the SwitchboardForm named FormToRun:

Code Snippet

static class Program

{

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

using (SwitchboardForm sf = new SwitchboardForm())

{

sf.ShowDialog();

if (sf.FormToRun != null)

{

Application.Run(sf.FormToRun);

}

}

}

}

Add to your SwitchboardForm code like the following:

Code Snippet

public Form FormToRun { get; private set; }

private void button1_Click(object sender, EventArgs e)

{

this.FormToRun = new FormToRun1();

this.DialogResult = DialogResult.OK;

}

private void button2_Click(object sender, EventArgs e)

{

this.FormToRun = new FormToRun2();

this.DialogResult = DialogResult.OK;

}

There is another way to accomplish this, using an inherited ApplicationContext, but it is more involved than the above.

BinaryCoder  Thursday, November 20, 2008 1:07 AM

Typically, in the Program.cs file, there is a line like the following:

Code Snippet
Application
.Run(new SwitchboardForm());

Whenever the form identified in this line closes, the application exits.

As a result of the Application.Run, this takes some cleverness to work around. The easiest thing to do would be to display your switchboard as a modal form prior to Application.Run. (Even though it is modal, it will not stop the user from switching to other applications. In other words, the user won't even notice that it is modal.) Here I show how this might work where the Form to be run is returned through a custom property of the SwitchboardForm named FormToRun:

Code Snippet

static class Program

{

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

using (SwitchboardForm sf = new SwitchboardForm())

{

sf.ShowDialog();

if (sf.FormToRun != null)

{

Application.Run(sf.FormToRun);

}

}

}

}

Add to your SwitchboardForm code like the following:

Code Snippet

public Form FormToRun { get; private set; }

private void button1_Click(object sender, EventArgs e)

{

this.FormToRun = new FormToRun1();

this.DialogResult = DialogResult.OK;

}

private void button2_Click(object sender, EventArgs e)

{

this.FormToRun = new FormToRun2();

this.DialogResult = DialogResult.OK;

}

There is another way to accomplish this, using an inherited ApplicationContext, but it is more involved than the above.

BinaryCoder  Thursday, November 20, 2008 1:07 AM

Hi Binary:

Wow! I had *no* idea you could run any process before the application run. Very cool. A real slick solution! I will give it a try. Thanks!

Kurt

Kurt Wimberger  Friday, November 21, 2008 3:51 PM

You can use google to search for other answers

Custom Search

More Threads

• Scrolling Panel via HScrollbar flickers
• User control property
• Window Proc
• Time callbacks, Invoke() or .BeginInvoke();
• Excel Automation Question - Charts
• Report Services
• Microsoft Word 2003 closes out
• Combo clear to refresh not allowed
• What's the story on UseCompatibleTextRendering
• calling a method when the app closes