C#/.NET - WinForms - Instantiate a Form without showing it

I am changing the Visibility of a Form to false during the load event AND the form still shows itself. What is the right event to tie this.Visible = false; to? I'd like to instantiate the Form1 without showing it.

using System;
using System.Windows.Forms;

namespace TestClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Visible = false;
        }

    }
}

Answers


Regardless of how much you try to set the Visible property before the form has been shown, it will pop up. As I understand it, this is because it is the MainForm of the current ApplicationContext. One way to have the form automatically load, but not show at application startup is to alter the Main method. By default, it looks something like this (.NET 2.0 VS2005):

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

If you instead do something like this, the application will start, load your form and run, but the form will not show:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Form1 f = new Form1();
    Application.Run();        

}

I am not entirely sure how this is useful, but I hope you know that ;o)

Update: it seems that you do not need to set the Visible property to false, or supply an ApplicationContext instance (that will be automatically created for you "under the hood"). Shortened the code accordingly.


I know this is an old question, but I just stumbled upon it and am pretty surprised no one has mentioned SetVisibleCore:

bool isVisibleCore = false;
protected override void SetVisibleCore(bool value)
{
    base.SetVisibleCore(isVisibleCore);
}

In that code snippet, as long as isVisibleCore remains false, the form will remain invisible. If it's set to false when the form is instantiated, you won't get that brief flash of visibility that you'd get if you set Visible = false in the Shown event.


It took me some time to find a properly working Solution.

Set the properties named WindowState to Minimized and ShowInTaskbar to False under properties window. Once your form is completly loaded, Call below lines of code.

        this.ShowInTaskbar = true;
        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        //this.WindowState = System.Windows.Forms.FormWindowState.Normal;

PS: This solution is Tested on Visual C# 2008 Express Edition


Just create an instance of Form1 and do not call methods to show/display it. But I bet you're doing something wrong.


How about setting the Opacity property to 0 on design and back to 100 when you want to show the form?


a solution i can live with so the form is created and on_load is called on createtion. set WindowState to minimize then on load set visible to false and windowstate to normal

private void Form1_Load(object sender, EventArgs e)
{
        this.Visible = false;
        this.WindowState = FormWindowState.Normal;
}

what did not worked:

the SetVisibleCore override solution did not created the form

as also the

Application {
 Form1 f = new Form1();
 Application.Run(); 

):


Try on the VisibleChanged event.


The shown event may give you want you want. Although the form will "flash" for a second before hiding.

private void Form1_Shown(object sender, EventArgs e)
    {
        this.Visible = false;
    }

What I would suggest would be to instantiate the form in an event the precedes the _Show event, such as the constructor, after the IntializeComponent() call.


If this is your main form, there may not be a better place then the Shown event. But in that case you will get flicker.

I couldn't find a good place to stop a running main form from showing at least quickly. Even a timer activated in the load event won't do it.

If it is a secondary form just create it but don't show it.


For a flicker-free Shown solution, also set the form's location off-screen during load:

private Point startuplocation;

private void Form1_Load(object sender, EventArgs e)
{
    this.startuplocation = this.Location;
    this.Location = new Point(-1000, -1000);
}

private void Form1_Shown(object sender, EventArgs e) //fires first time shown
{
    this.Visible = false;
    this.Location = this.startuplocation;
}

Have you tried

this.Hide();

in the form_load or form_activated events


Set the visibilty on the constructor, after init and then this.Show() later


InitializeComponent() is setting this.Visible = true, since you specified that the form should be visible in the designer (or it defaulted to that). You should set Visible to false in the designer, and it won't be called by InitializeComponent(). You can then make it visible any time you like.


Having .Visible = false or Hide() in the Load event will cause your form to show briefly, as there is time between when it becomes physically visible and when the Load event gets fired, in spite of the fact that the documentation says the contrary.

Are you calling Show() or ShowDialog() somewhere? I'm not sure if this behavior is still present, but at least in past versions of the framework a call to ShowDialog() did not trigger the Load event, so perhaps that is your issue (though I think calling ShowDialog() then hiding a modal form would be a bad practice!)

If you have to have the handle created (and the handles for controls) for whatever it is you're trying to do, a better idea would be to set the StartLocation to Manual, then set the Position property to an offscreen location. This will create and show the form, while making it invisible to the user.


Yeah the really one elegant way in perspective to your code than your applications visual is to flicker the form by hiding in the constructor/load event.


I agree that this can be really maddening, because Winforms usually don't look pretty while they're initializing a bunch of controls or populating a big DataGridView or whatever. Still you do need the window handle to exist before you can do that, creating all the issues that have been mentioned.

Here's something that has worked for me, and you have two choices: You can just hide your main form until it's ready, or you can show some kind of little splash screen to let your user know that you're working on it. Enjoy.

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }
    Size mDeferredSize;
    protected override void OnHandleCreated(EventArgs e)
    {
        // Capture the "real" size...
        mDeferredSize = Size;
        // and set it to nothing...
        Size = new Size(0, 0);
        DoSomeUglyInitialization(showOptionalSplash: true);
        Size = mDeferredSize; // ...and now put it back to the original size    
        base.OnHandleCreated(e);
    }
    private void DoSomeUglyInitialization(bool showOptionalSplash)
    {
        MySplash splash = null; 
        if (showOptionalSplash)
        {
            // We have made some borderless form class with a logo or something...
            splash = new MySplash(); 
            splash.Show();
        }
        // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        // Initialization that REQUIRES A HANDLE, e,g,
        // we might be creating a bunch of controls, or 
        // populating a big DataGridView. Whatever it is,
        // we don't want everyone looking at our biz.
        System.Threading.Thread.Sleep(2500); // (Here simulated...)
        // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        splash?.Dispose();      
    }
}

I set these three property settings for the form:

ShowInTaskbar = false
ShowIcon = false
WindowState = Minimized

Need Your Help

How can I get the iOS 7 default blue color programmatically?

ios colors ios7 uicolor

I'm creating custom elements in my app and want to match the look and feel of the new iOS. iOS 7 introduced to us a very common lighter blue color, the default color or tint for several elements,

Returning from a method with implicit or explicit "else" or with a single "return" statement?

java coding-style

Some people consider multiple return statements as bad programming style. While this is true for larger methods, I'm not sure if it is acceptable for short ones. But there is another question: Should