16

I wanted to hide the main window of my app on startup, so I put this in the constructor:

this.Hide();

This doesn't hide my form though. It seems like I can only get buttons to hide the form. Am I doing something wrong here?

Pieter
  • 31,619
  • 76
  • 167
  • 242
  • possible duplicate of [this.Visible is not working in Windows Forms](http://stackoverflow.com/questions/3742709/this-visible-is-not-working-in-windows-forms) – Hans Passant Sep 22 '10 at 14:57

8 Answers8

31

you can use this line of code. It wont hide it, but it will be minimized:

this.WindowState = FormWindowState.Minimized;

in addition, if you don't want it showing on the task bar either, you can add this line:

this.ShowInTaskbar = false;

But why do you create the form if you don't want it to be visible in the first place?

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • Because it will start with Windows and reside in the system tray in a way similar to antivirus and firewall software. Any idea as to why `this.Hide()` doesn't actually hide the form when called from the form constructor? – Pieter Sep 22 '10 at 21:05
  • 1
    I think this is because at this point (when running the constructor) your form has not been made visible yet. Only after the constructor is done, the form will be created and made visible. So your only problem why this does not work is because it's done from the constructor. I don't suggest this as a solution, but as an experiment, make a timer and start it from the constructor with a 1 second delay, and have the timer_tick method hide the form. This will work, since at the time you call this.Hide() the form will be visible. – Øyvind Bråthen Sep 23 '10 at 07:03
  • You get the same problem when you have a construct like this in program.cs: Application.Run(new Form1()); and you try to write Application.Exit() in the constructor of Form1. After the Application.Exit() call your application will still be alive and well, and that is because the constructor is run before Application.Run, and thereby will have no effect. So same problem for you. You make it Hidden, just to have Application.Run make it vivible and kicking again. – Øyvind Bråthen Sep 23 '10 at 07:06
  • 1
    I went with your solution, but I also attached a method to the form's `Load` event so that I can hide the window properly using `this.Hide()` once the form has been loaded. Thanks! – Pieter Sep 23 '10 at 10:33
  • Form.Load will be a good place for this probably. One questions since I haven't tried myself. Does the form first appear, and then get hidden so you experience a flicker if you hide it from the Load event if you don't minimize it in the constructor? – Øyvind Bråthen Sep 23 '10 at 12:07
14

Just override the OnVisibleChanged method and change the visibility of the form in there, something like this:

protected override void OnVisibleChanged(EventArgs e)
{
    base.OnVisibleChanged(e);
    this.Visible = false;
}

And that's it! Simple and clean.

abraxas005
  • 246
  • 3
  • 3
5

If you would rather use this.Hide or this.Show you can do this

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    this.Hide();
}
Geens
  • 139
  • 1
  • 4
2

I tried to do this by setting Visible to false or hiding in the constructor and in the OnLoad event.

Neither of these had any effect, as the form is set to Visible after the form is created and after the OnLoad event is fired, in SetVisibleCore.

Setting the form to hidden in the Shown event works, but the form flickers on the screen for a moment.

You can also override the SetVisibleCore and set the value to false, but then OnLoad isn't fired and some of the other events are messed up, such as form closing.

The best solution in my opinion is to set the form to minimised and not shown in the taskbar before calling Application.Run().

So instead of:

Application.Run(new MainForm());

do:

MainForm form = new MainForm();
form.WindowState = FormWindowState.Minimized;
form.ShowInTaskbar = false;

Application.Run(form);

Then the application will run with all the proper events fired (even OnShown) and the form will not be displayed.

If you want to be able to hide / show the form like normal after that, then you need to set the WindowState and ShowInTaskbar back to Normal and true.

In the Shown event, you can set ShownInTaskbar back to true and then properly hide the form.

this.Shown += new System.EventHandler(this.MainFormShown);

...

void MainFormShown(object sender, EventArgs e)
{
    this.ShowInTaskbar = true;
    this.Visible = false;
}

Settings the WindowState back to Normal whilst the form is hidden has no effect, so you will need to do it after you show the form again, otherwise the icon will be in the taskbar but the form will be minimised.

this.Show();
this.WindowState = FormWindowState.Normal;
Jack Culhane
  • 763
  • 7
  • 11
  • 1
    The working solution solution above did not work for me. It caused the window to flicker on screen momentarily before being hidden. Also it prevents you from showing the form afterwards. This method did work fine for me. – Jack Culhane Jul 17 '13 at 08:49
0

Try setting the visible property of the form to false before it is loaded in the main entry point of your application.

Form1 obj = new Form1();
obj.visible = false;
Application.Run(obj);

Or try setting the co-ordinates of the form to higher location like 9000, 9000.

Sidharth Panwar
  • 4,606
  • 6
  • 27
  • 36
0

You can start a form hidden using Form.Hide():

Form2 form = new Form2();
//Start Form2 hidden
form.Hide();
//Close Form2
form.Close();
Xbold
  • 1
  • 1
0

You can create a firstOpen bool variable in the form and make this in the Show event

    private void Form1_Shown(object sender, EventArgs e)
    {
        if (firstOpen)
        {
            this.WindowState = FormWindowState.Minimized;
            Hide();
            notifyIcon1.Visible = true;
        }
        firstOpen = false;
    }

In the first time it is showed, it will be hidded. Next time will works normal.

LeandroIP
  • 41
  • 3
0

To hide forms on start up, it's actually very simple. You may create the form and store it in a local variable, and simply does not put the form in Application.Run() so that it is not rendered on startup:

Form1 form = new Form1(); 
Application.Run();  

Øyvind is right on that calling this.Hide() from the constructor would not work because the form is not ready yet. In this way, since the form is already created, you may call this.Show() in any function of the form, and call this.Hide() if you want to hide it.