Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this.Visible is not working in Windows Forms

Tags:

I have a problem. I need to hide my window at window load. But

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

is not working. And property Visible remains true. Am I missing something?

like image 901
Barun Avatar asked Sep 18 '10 17:09

Barun


People also ask

How do I show text in Windows Forms?

Step 1: Create a windows form. Step 2: Drag the TextBox control from the ToolBox and Drop it on the windows form. You can place TextBox anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the TextBox control to set the Text property of the TextBox.


2 Answers

Yes, the Visible property is a big deal in Windows Forms, that's what actually gets the handle created and causes OnLoad() to run. In other words, the window doesn't exist until it gets visible. And it will ignore attempts to undo this.

It is pretty common to want to still create the handle but not make the window visible if you use a NotifyIcon. You can achieve this by overriding SetVisibleCore:

protected override void SetVisibleCore(bool value) {
    if (!this.IsHandleCreated) {
        value = false;
        CreateHandle();
    }
    base.SetVisibleCore(value);
}

Beware that OnLoad still won't run until the window actually gets visible so move code into the constructor if necessary. Just call Show() in the NotifyIcon's context menu event handler to make the window visible.

like image 196
Hans Passant Avatar answered Sep 28 '22 05:09

Hans Passant


It seems you can use the following:

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

I just tested it in a winforms app and it worked.

(Also just found this: Single Form Hide on Startup)

like image 20
mpeterson Avatar answered Sep 28 '22 05:09

mpeterson