views:

173

answers:

7

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?

A: 

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

Shantanu Gupta
ok. but what is wrong with visible ?
Barun
this.Hide will not work inside the _Load event
pm_2
A: 

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);
        }
    }
pm_2
I tried it. But before hide It appearing and then disappearing. And I dont want it.
Barun
Then I think you have to do whatever checks you have before you show the form. Note: even if this is your main form, you can use the Main() function to do this check.
pm_2
But I need this from hide and running and I cant do my things before starting it.
Barun
If you have functions that you need to use then you could expose them, instantiate the form and call the functions from outside the form. See edited answer.
pm_2
A: 

Use this.Opacity = 0;

cprcrack
A: 

I think it is not good idea to set visibility from form's Load event. Instead, I would do it by add a public method:

public void LoadForm(...)
{
   // do the all the initializations
}

and call the method to load the form. The form should be not visible unless you explicitly show it:

MyForm instance = new MyForm();
instance.LoadForm(...);
// instance.Show(); or ShowDialog() for dialog form not sure exactly the syntax.
David.Chu.ca
But this is my main form how can I control it with this syntax ?
Barun
+3  A: 

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

mpeterson
A: 

Put your call in Windows event loop like this:

WindowsFormsSynchronizationContext.Current.Post((obj) => this.Hide(), null);

So Hide() will be invoked later, hence fix your problem.

tia
+7  A: 

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 if necessary. Just call Show() in the NotifyIcon's context menu event handler to make the window visible.

Hans Passant