Baran Topal

Baran Topal


April 2024
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
2930  

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.