Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding forms on startup: why doesn't this.Hide() hide my form?

Tags:

c#

forms

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?

like image 469
Pieter Avatar asked Sep 22 '10 12:09

Pieter


People also ask

How do you make a form not visible?

Press F5 to build and run the application. Click on the button in the main form to display the sub form. Now, when you press the button in the sub form, the form will be hidden.

How do I remove a form from my taskbar?

To prevent your form from appearing in the taskbar, set its ShowInTaskbar property to False.

How do I hide a form in winform?

If you Don't want the user to be able to see the app at all set this: this. ShowInTaskbar = false; Then they won't be able to see the form in the task bar and it will be invisible.

How do you make a form invisible in Visual Studio?

Use Me. Opacity = 0 to hide the form on load event.


3 Answers

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?

like image 187
Øyvind Bråthen Avatar answered Oct 16 '22 10:10

Øyvind Bråthen


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();
}
like image 29
Geens Avatar answered Oct 16 '22 09:10

Geens


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.

like image 30
abraxas005 Avatar answered Oct 16 '22 11:10

abraxas005