Multiple windows forms handling - problem

S

Sudha Pune

Hi all

I have 3 windows forms and I have one user control which holds a menu
like below

FormA | FormB | FormC

This menu will displayed at top of the all the forms

If i click FormB in FormA's menu i want to close the formA and i want
to show the FormB

In what way i can achieve this,

And how can i have the reference of loaded FormA reference in FormB

Please post a code snippet

Thanks
 
G

Guest

You might wish to consider starting your application using an
ApplicationContext object which can manage Application startup/shutdown and
the interaction between your forms. If you have state which needs to be
shared between forms then I would suggest you manage this state in another
class.

On a side note, it sounds like you are building a UI with multiple windows
but no main form. This design might prove confusing to your users.

HTH

kh
 
S

Sudha Pune

Can anyone post the ideas about managing the forms using
ApplicationContext for my above scenario

Hi kh

Thanks for ur reply and my client has given design spec like that. So
FormA will act as main form.
 
C

Chris Jobson

Sudha Pune said:
Can anyone post the ideas about managing the forms using
ApplicationContext for my above scenario

OK, I've got a sample working. Create a normal VS2005 Windows Application
and add a second form. Then edit the Program.cs file as follows:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsApplication1
{
class MyApplicationContext : ApplicationContext
{
public MyApplicationContext()
{
Form1 f1 = new Form1();
f1.FormClosing += new
FormClosingEventHandler(form1_FormClosing);
f1.Show();
}

void form1_FormClosing(object sender, FormClosingEventArgs e)
{
Form f2 = new Form2();
MainForm = f2;
f2.Show();
}
}

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyApplicationContext());
}
}
}

Now when you run the application it displays Form1. When you close Form1 it
displays Form2. When you close Form2 the application closes.

Chris Jobson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top