this.Visible is not working in Windows Forms

2019-01-03 16:53发布

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?

9条回答
闹够了就滚
2楼-- · 2019-01-03 17:51

Use this.Hide() to hide your window. this.Close() to close

查看更多
贪生不怕死
3楼-- · 2019-01-03 17:54

I believe this is because the window doesn't really exist until after this event. The best place to do this is outside the form:

if (x == 1)
{
    form1.ShowForm();
}
else
{
    // Don't show the form
}

If you really need to do it inside the form itself, then I think you need to use the Activated event.

EDIT:

You could also try something like:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form1 myform = new Form1();
        if (myform.CheckStuff())
        {
            Application.Run(myform);
        }
    }
查看更多
放我归山
4楼-- · 2019-01-03 18:00

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

查看更多
登录 后发表回答