Baran Topal

Baran Topal


September 2018
M T W T F S S
« Feb    
 12
3456789
10111213141516
17181920212223
24252627282930

Categories


another form before main form in C#

baranbaran

I remember that I needed yet another form before the main form. The elegant way to do that is as follows:


static class Program
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 f1 = new Form1();
            f1.Opacity = 0; // load it hidden
            Form2 f2 = new Form2();
            f2.ControlBox = false;
            f2.FormClosed += delegate(object sender, FormClosedEventArgs e) { f1.Opacity = 1; f2.Dispose(); };
            f2.Show();
            Application.Run(f1);
        }

    }

Above Form2 is the “new form” with the button on it. I set the ControlBox() to false so I have to use the button to close it. The button handler simply calls “this.Close()”.

I’ve set the Opacity() of Form1, the “old form”, to 0 (zero) to make it load invisible. When Form2 is closed I set the Opacity to 1 (one) to make it appear.