Click here to Skip to main content
13,353,124 members (64,822 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


24 bookmarked
Posted 21 Jan 2010

Multiple Subsequent "Main" Forms in C# Apps

Rate this:
Please Sign up or sign in to vote.
A useful technique that could be used for winform apps which require a login dialog.
In this Quick Answer question[^], aspdotnetdev recommended to the original poster to run Application.Run twice. I had never thought of doing that before, but I actually found a reason to do so in my own development process (aspdotnet gave me divine dispensation to post a tip/trick about it, but I needed a real-world example before I could do it - here's that example).

We have an application that requires the user to login. My solution was based on this little info gem.

First, I created a login form. In the form I placed the requisite userID/password textboxes, and a Login and Cancel buttons. The handlers for these buttons set the DialogResult property of the form (Login sets it to DialogResult.OK and Cancel sets it to DialogResult.Cancel).

Then, in program.cs, I have the following code:

static class Program
    private static FormSplash m_formSplash = null; 

    static void Main()

        m_formSplash = new FormSplash();
        if (m_formSplash.DialogResult == DialogResult.OK)
            Application.Run(new FormMain());

What happens is that the splash form runs, and when it exits, the application class itself determines whether or not to exit the app or run the next form.

This sure beats running the main form and having special processing in that form to handle the login form. It's A LOT cleaner (code abstraction/separation is almost always a good thing), and easier to maintain, especially if there are copious comments in the Form classes to notify the maintenance programmer (or remind the original programmer) what's happening, and why.


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

GeneralReason for my vote of 5 It works great. Pin
Mangore23-Jul-10 10:03
memberMangore23-Jul-10 10:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 22 Nov 2011
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid