Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Multiple User-Selectable Main Forms

0.00/5 (No votes)
22 Nov 2011 1  
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).

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:

//--------------------------------------------------------------------------------
// 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:

//--------------------------------------------------------------------------------
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here