[ACCEPTED]-Load a form without showing it-winforms

Accepted answer
Score: 19

Perhaps it should be noted here that you 9 can cause the form's window to be created without 8 showing the form. I think there could be 7 legitimate situations for wanting to do 6 this.

Anyway, good design or not, you can 5 do that like this:

MyForm f = new MyForm();
IntPtr dummy = f.Handle; // forces the form Control to be created

I don't think this will 4 cause Form_Load() to be called, but you 3 will be able to call f.Invoke() at this 2 point (which is what I was trying to do 1 when I stumbled upon this SO question).

Score: 8

It sounds to me like you need to sit down 4 and re-think your approach here. I cannot 3 imagine a single reason your public methods 2 need to be in a form if you are not going 1 to show it. Just make a new class.

Score: 6

I totally agree with Rich B, you need to 13 look at where you are placing your application 12 logic rather than trying to cludge the WinForms 11 mechanisms. All of those operations and 10 data that your Tasks form is exposing should 9 really be in a separate class say some kind 8 of Application Controller or something held 7 by your main form and then used by your 6 tasks form to read and display data when 5 needed but doesn't need a form to be instantiated 4 to exist.

It probably seems a pain to rework 3 it, but you'll be improving the structure 2 of the app and making it more maintainable 1 etc.

Score: 5

From MSDN:

Form.Load
Occurs before a form is displayed 11 for the first time.

Meaning the only thing 10 that would cause the form to load, is when 9 it is displayed.
Form.Show(); and Form.Visible = true; are the exact same 8 thing. Basically, behind the scenes, Show 7 checks for various conditions, then sets 6 Visible to true. So obviously, setting visible 5 to false (which it already is) before showing 4 the form is meaningless.

But let's forget 3 the technicalities. I completely agree with 2 Rich B and Shaun Austin - the logic shouldn't 1 be in that form anyway.

Score: 1

Sometimes this would be useful without it 16 being bad design. Sometimes it could be 15 the start of a migration from native to 14 managed.

If you were migrating a c++ app 13 to .NET for example, you may simply make 12 yourwhole app a child window of the .NET 11 form or panel, and gradually migrate over 10 to the .NET by getting rid of your c++ app 9 menu, status bar, toolbar and mapping teh 8 .NEt ones to your app using platform invoke 7 etc...

Your C++ app may take a while to load, but 6 the .NET form doesn't..in which you may 5 like to hide the .NEt form until your c++ app 4 has initialised itself.

I'd set opacity=0 3 and visible=false to false after calling 2 show, then when your c++ app loads, then 1 reverse.

Score: 0

If you make the method public, then you 5 could access it directly.... however, there 4 could be some unexpected side effects when 3 you call it. But making it public and calling 2 it directly will not draw the screen or 1 open the form.

Score: 0

Move mandatory initialization code for the 7 form class out of the Load event handler into 6 the constructor. For a Form class, instantiation 5 of an instance (via the constructor), form 4 loading and form visibility are three different 3 things, and don't need to happen at the 2 same time (although they do obviously need 1 to happen in that order).

Score: 0

None of the answers solved the original 4 question, so, add the below, call .Show() to 3 load the form without showing it, then call 2 .ShowForm() to allow it to be visible if 1 you want to after:

private volatile bool _formVisible;
protected override void SetVisibleCore(bool value)
{
    base.SetVisibleCore(_formVisible);
}
public void ShowForm()
{
    _formVisible = true;
    if (InvokeRequired)
    {
        Invoke((Action) Show);
    }
    else
    {
        Show();
    }
}

More Related questions