tags:

views:

1356

answers:

5

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:

tasksForm.Visible = false;
tasksForm.Show();

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.

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:

tasksForm.WindowState = FormWindowState.Minimized;
tasksForm.Show();

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.

+8  A: 

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.

Geoffrey Chetwood
To extrapolate on your point, have the Model class for the Task, and the View/Controller class which is the TaskForm.
sixlettervariables
@sixlettervariables: Why would he need a View? He said he isn't going to load the form anyway. Sounds like he just need a class.
Geoffrey Chetwood
I missed the part where he wasn't even going to view the data. Kind of a non-starter question if that is the only question...
sixlettervariables
A: 

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.

Sam
A: 

Move mandatory initialization code for the form class out of the Load 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).

McKenzieG1
+5  A: 

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.

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

Shaun Austin
*sigh* OK, I'll be good. Thanks for the feedback, everybody.
Don Kirkby
+1  A: 

From MSDN:

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

Meaning the only thing that would cause the form to load, is when it is displayed.
Form.Show(); and Form.Visible = true; 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.

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

configurator