0

I've always used this.Hide in Form1_Load(...) when I needed to and it has always worked. But now in a new project, this.Hide does not work anywhere and this is the only code I have!

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

Why doesn't the Form Hide?

DIF
  • 2,470
  • 6
  • 35
  • 49
spike.y
  • 389
  • 5
  • 17
  • The real question is: why do you want to hide something when you are showing it? Smells bad. You surely don't need to call `Show`/`ShowDialog` if you are not intending to show form. – Sinatr May 26 '14 at 09:35
  • @Sinatr no need to be paranoid. It prevents you from seeing genuinely good intentions. – spike.y May 26 '14 at 10:43
  • 1
    Good intentions can be painfully implemented. I am still curious why do you need to hide form in `Form1_Load(...)`. – Sinatr May 26 '14 at 11:14

5 Answers5

1

It's not working as you call it in the Load. The load happens before it actually becomes visible, so you can't hide it from there either.

Also you should search before asking:

why isn't this.Hide() working in Form1_load event?

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

Community
  • 1
  • 1
fishmong3r
  • 1,414
  • 4
  • 24
  • 51
1

The Load event is too early to hide the Form since it isn't shown yet then.

When overriding the OnShown method it does work for me:

protected override void OnShown(EventArgs e)
{
    base.OnShown(e);

    this.Visible = false;
    this.Hide();
}

Or, of course, create a handler for the Shown event.

Either of the two calls is good. You don't need both.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

From here:

private bool mShowAllowed;
protected override void SetVisibleCore(bool value)
{
    if (!mShowAllowed) value = false;
    {
        base.SetVisibleCore(value); 
    }
}
AgentFire
  • 8,944
  • 8
  • 43
  • 90
0

Or you can simply hide in the Shown event of the Form1:

private void Form1_Shown(object sender, EventArgs e)
{
    this.Hide();
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Hassan
  • 5,360
  • 2
  • 22
  • 35
  • Yeah I know I can do that, but if you hide it in the Shown event, 90% of the time you see the form flash before it gets hidden. Very annoying. – spike.y May 26 '14 at 09:24
0

For me this is working.

private void Form1_Load(object sender, EventArgs e)
{
    this.Opacity = 0;
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Ede Troll
  • 16
  • 3