<p>Short version: I want to trigger the Form_Load() event without making the form visible. This doesn't work because Show() ignores the current value of the Visible property:</p>

<pre><code>tasksForm.Visible = false;
tasksForm.Show();
</code></pre>

<p>Long version: I have a WinForms application with two forms: main and tasks. The main form is always displayed. The user can either click a button to open the tasks form, or click some buttons that just run a task directly without opening the tasks form.</p>

<p>When a user asks to run a task directly, I'd like to just call some public methods on the tasks form without showing it. Unfortunately, the task logic depends on stuff that happens in the Form_Load() event. The only way I can find to trigger Form_Load() is to call Show(). The best I've been able to do is to show the form in the minimized state:</p>

<pre><code>tasksForm.WindowState = FormWindowState.Minimized;
tasksForm.Show();
</code></pre>

<p>I suppose the cleanest solution would be to pull the tasks logic out of the tasks form and into a controller class. Then I can use that class from the main form and from the tasks form, and only load the tasks form when I need it visible for the user. However, if it's an easy thing to load the form without displaying it, that would be a smaller change.</p>

[{'answer_id': 66945, 'author': 'GEOCHET', 'author_id': 5640, 'author_profile': 'https://Stackoverflow.com/users/5640', 'pm_score': 3, 'selected': False, 'text': '<p>It sounds to me like you need to sit down and re-think your approach here. I cannot imagine a single reason your public methods need to be in a form if you are not going to show it. Just make a new class.</p>\n'}, {'answer_id': 66975, 'author': 'Sam', 'author_id': 9406, 'author_profile': 'https://Stackoverflow.com/users/9406', 'pm_score': 0, 'selected': False, 'text': '<p>If you make the method public, then you could access it directly.... however, there could be some unexpected side effects when you call it. But making it public and calling it directly will not draw the screen or open the form.</p>\n'}, {'answer_id': 66991, 'author': 'McKenzieG1', 'author_id': 3776, 'author_profile': 'https://Stackoverflow.com/users/3776', 'pm_score': 0, 'selected': False, 'text': "<p>Move mandatory initialization code for the form class out of the <code>Load</code> event handler into the constructor. For a Form class, instantiation of an instance (via the constructor), form loading and form visibility are three different things, and don't need to happen at the same time (although they do obviously need to happen in that order).</p>\n"}, {'answer_id': 67019, 'author': 'Shaun Austin', 'author_id': 1120, 'author_profile': 'https://Stackoverflow.com/users/1120', 'pm_score': 4, 'selected': True, 'text': "<p>I totally agree with Rich B, you need to look at where you are placing your application logic rather than trying to cludge the WinForms mechanisms. All of those operations and data that your Tasks form is exposing should really be in a separate class say some kind of Application Controller or something held by your main form and then used by your tasks form to read and display data when needed but doesn't need a form to be instantiated to exist. </p>\n\n<p>It probably seems a pain to rework it, but you'll be improving the structure of the app and making it more maintainable etc.</p>\n"}, {'answer_id': 67592, 'author': 'configurator', 'author_id': 9536, 'author_profile': 'https://Stackoverflow.com/users/9536', 'pm_score': 3, 'selected': False, 'text': "<p>From MSDN:</p>\n\n<blockquote>\n  <p><strong>Form.Load</strong><br>\n  Occurs before a form is displayed for the first time. </p>\n</blockquote>\n\n<p>Meaning the only thing that would cause the form to load, is when it is displayed.<br>\n<code>Form.Show();</code> and <code>Form.Visible = true;</code> are the exact same thing. Basically, behind the scenes, Show checks for various conditions, then sets Visible to true. So obviously, setting visible to false (which it already is) before showing the form is meaningless.</p>\n\n<p>But let's forget the technicalities. I completely agree with Rich B and Shaun Austin - the logic shouldn't be in that form anyway.</p>\n"}, {'answer_id': 4411493, 'author': 'Jeff Roe', 'author_id': 253586, 'author_profile': 'https://Stackoverflow.com/users/253586', 'pm_score': 4, 'selected': False, 'text': "<p>Perhaps it should be noted here that you <strong>can</strong> cause the form's window to be created without showing the form.  I think there could be legitimate situations for wanting to do this.</p>\n\n<p>Anyway, good design or not, you can do that like this:</p>\n\n<pre><code>MyForm f = new MyForm();\nIntPtr dummy = f.Handle; // forces the form Control to be created\n</code></pre>\n\n<p>I don't think this will cause Form_Load() to be called, but you will be able to call f.Invoke() at this point (which is what I was trying to do when I stumbled upon this SO question).</p>\n"}, {'answer_id': 18763439, 'author': 'Ben Glancy', 'author_id': 2772483, 'author_profile': 'https://Stackoverflow.com/users/2772483', 'pm_score': 1, 'selected': False, 'text': "<p>Sometimes this would be useful without it being bad design. Sometimes it could be the start of a migration from native to managed.</p>\n\n<p>If you were migrating a c++ app to .NET for example, you may simply make yourwhole  app a child window of the .NET form or panel, and gradually migrate over to the .NET by getting rid of your c++ app menu, status bar, toolbar and mapping teh .NEt ones to your app using platform invoke etc...</p>\n\n<p>Your C++ app may take a while to load, but the .NET form doesn't..in which you may like to hide the .NEt form until your c++ app has initialised itself.</p>\n\n<p>I'd set opacity=0 and visible=false to false after calling show, then when your c++ app loads, then reverse.</p>\n"}, {'answer_id': 51967639, 'author': 'Sodoshi', 'author_id': 3820052, 'author_profile': 'https://Stackoverflow.com/users/3820052', 'pm_score': 0, 'selected': False, 'text': '<p>None of the answers solved the original question, so, add the below, call .Show() to load the form without showing it, then call .ShowForm() to allow it to be visible if you want to after:</p>\n\n<pre><code>private volatile bool _formVisible;\nprotected override void SetVisibleCore(bool value)\n{\n    base.SetVisibleCore(_formVisible);\n}\npublic void ShowForm()\n{\n    _formVisible = true;\n    if (InvokeRequired)\n    {\n        Invoke((Action) Show);\n    }\n    else\n    {\n        Show();\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/66921', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4794/']