Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Multiple User-Selectable Main Forms

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
22 Nov 2011CPOL 15.3K   3  
Allow any number of forms to be used as a main form with easy transitions between forms

While working on CamoPicker, I decided I needed to display one of two forms when the program started up, and while the program was running, allow the user to change to the other form if desired. I used a Settings object to save the currently selected form, and treat both forms as modeless forms. That way, initialization only happens once (a requirement in my app), and the forms are easily displayed by subsequent user interaction. The necessary code bits follow. Both forms contain the same code with appropriate class name replacements where indicated.

In Program.cs, I loaded the appropriate form (saved during the last program execution).

C#
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    switch (Settings1.Default.LastForm)
    {
        case "Form2" :
            Application.Run(new Form2());
            break;
        default :
            Application.Run(new Form1());
            break;
    }
}

In the form classes, I handled the Closed event and added a method to show the form if it had already been shown once:

C#
//--------------------------------------------------------------------------------
// This event is fired when the user closes the form. As expected, when you click 
// the appropriate button (or the "X" button in the titlebar), the app closes. In 
// this case, we also have to close the other form in case there's any cleanup 
// that needs to happen in it.
private void btnClose_Click(object sender, EventArgs e)
{
    foreach(Form otherForm in Application.OpenForms)
    {
	    // in the other form (Form2), the following line would check to see if 
		// "otherform is Form1"
        if (otherForm is Form2)
        {
            otherForm.Close();
        }
    }
    Application.Exit();
}

//--------------------------------------------------------------------------------
// Allows us to show the other form WITH suitable sanity checking prior to it 
// being shown.
public void ShowAgain()
{
    this.Cursor = Cursors.WaitCursor;
	// perform sanity checking here (if necessary)


	// Here, we save the form that's being show. In the other form (Form2), the 
	// following line sets LastForm = "Form2".
    Settings1.Default.LastForm = "Form1";
    Settings1.Default.Save();
    this.Cursor = Cursors.Default;
    this.Show();
}

Finally, I use a button on each form to show the other form:

C#
//--------------------------------------------------------------------------------
private void btnShowForm2_Click(object sender, EventArgs e)
{
    FormAdvanced otherForm  = null;
    bool         formExists = false;

	// enumerate the currently open forms
    foreach(Form form in Application.OpenForms)
    {
	    // if we found the form we want
        if (form is Form2)
        {
		    // cast it (in Form2, the next line would be looking for a Form1 object)
            otherForm  = form as Form2;
            // indicate that we found it
            formExists = true;
			// break out of the loop
            break;
        }
    }
	// if the form doesn't exist
    if (!formExists)
    {
	    // create it, and show it
        otherForm = new Form2();
        otherForm.Show();
    }
    else
    {
	    // otherwise, show it again
        otherForm.ShowAgain();
    }
	// hide this form
    this.Hide();
}

The net effect is that I only have one form at a time displayed for the app, yet I can have as many different forms as necessary. In my case, I only needed two, but you might have different requirements.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
-- There are no messages in this forum --