Click here to Skip to main content
Click here to Skip to main content

Multiple Subsequent "Main" Forms in C# Apps

By , 22 Nov 2011
 
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; 
 
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
 
        m_formSplash = new FormSplash();
        Application.Run(m_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.

License

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)
United States United States
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberkaandemirtas6 May '13 - 10:18 
GeneralMy vote of 5memberj-fux19 May '12 - 17:56 
QuestionIs this OK?membersPhinX12 Apr '12 - 23:35 
GeneralReason for my vote of 5 Really very nice.memberAhsanS2 Feb '12 - 18:37 
GeneralReason for my vote of 4 I use this method in my project toomemberzf.liu1 Dec '11 - 4:03 
GeneralReason for my vote of 5 not special, but super usefull 5memberb1054374829 Nov '11 - 3:28 
GeneralThe code is using Application.Run() two times. That means tw...memberAhmed Naqibul Arefin22 Nov '11 - 19:19 
GeneralRe: No, only one instance of the application is running.mvpJohn Simmons / outlaw programmer30 Nov '11 - 11:19 
GeneralReason for my vote of 5 Good one!membervallarasus22 Nov '11 - 5:49 
GeneralVery nice trick/tip. Thanksmemberzenwalker19856 Nov '11 - 2:42 
GeneralReason for my vote of 4 always good to understand the code t...memberlxnjob10 Oct '11 - 5:16 
GeneralReason for my vote of 5 5 always good to understand the co...memberEddy Vluggen8 Oct '11 - 15:37 
GeneralReason for my vote of 4 Great Example but, I would set the C...membercharles henington5 Jul '11 - 15:24 
GeneralVery useful and simple, thanks...memberH.Johnson24 May '11 - 3:35 
GeneralThere is nothing special about this design of Forms applicat...mvpSAKryukov15 May '11 - 12:12 
There is nothing special about this design of Forms application.
However, there are many (mostly stupid) questions on CodeProjects Questions-and-Answer which could be resolved by just referring to this little article. My 5.
--SA
GeneralReason for my vote of 5 I really found it very usefullmembersbcomputers10 Mar '11 - 21:26 
GeneralSuperb.memberToli Cuturicu20 Dec '10 - 12:26 
GeneralReason for my vote of 5 Thanks, John. It does indeed work ni...subeditorWalt Fair, Jr.10 Dec '10 - 12:55 
GeneralReason for my vote of 5 It works great.memberMangore23 Jul '10 - 9:03 
GeneralLooks Goodmemberaspdotnetdev23 Jan '10 - 21:13 
GeneralRe: Looks GoodmemberJohn Simmons / outlaw programmer24 Jan '10 - 2:14 
GeneralNice tipmemberAbhinav S23 Jan '10 - 3:49 
GeneralCould this be used to handle exceptionsmembersupercat922 Jan '10 - 7:26 
GeneralCool, but could be titled bettermemberJ. Dunlap21 Jan '10 - 8:33 
GeneralRe: Cool, but could be titled bettermemberJohn Simmons / outlaw programmer21 Jan '10 - 23:46 

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

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