Click here to Skip to main content
13,507,150 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 22 Nov 2011

Multiple User-Selectable Main Forms

Rate this:
Please Sign up or sign in to vote.
Allow any nnumber 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()
    switch (Settings1.Default.LastForm)
        case "Form2" :
            Application.Run(new Form2());
        default :
            Application.Run(new Form1());

In the form classes, I handled the Closed event and added a method to show the form if 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)

// 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";
    this.Cursor = Cursors.Default;

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
	// if the form doesn't exist
    if (!formExists)
	    // create it, and show it
        otherForm = new Form2();
	    // otherwise, show it again
	// hide this form

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.


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


About the Author

John Simmons / outlaw programmer
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.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180417.1 | Last Updated 22 Nov 2011
Article Copyright 2011 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid