In Visual Studio 2012 why is a message box in the project shown as the project loads?

时间:2015-07-28 23:16:18

标签: c# winforms visual-studio-2012 messagebox

In Visual Studio 2012, I added a message box to the load event of a WinForms user control that will pop up if the user control was not initialized correctly (when the load event is fired). All the check does is see if a couple of member variables in the user control are null or not.

The user control is in a different project from the main application because it will be used by more than one application.

The intended sequence of events is this: In the main application, there is a right click menu with an option to show a window. That window contains the user control being referred to. When the user selects that menu item it creates an instance of the window, initializes the window through an initialization method, which in turn sets the variables in the user control to initialize it. Then the form.Activate method is called, then ShowDialog() and Focus() for the window containing the user control.

Here's the issue: When I load the solution in Visual Studio 2012, while the solution is being loaded and before any code is shown, the message box is sometimes shown as a modal dialog of Visual Studio itself. The solution does not continue to load until I dismiss the message box.

Also, the message box is sometimes shown when I debug the application, popping up immediately after the login window of the application is shown. I can't seem to find any pattern to why the message box is shown in these two instances.

This is very mystifying as the code is not even executing, yet the message box is being shown. And during application start up the message box is shown even though the right click menu has not been selected.

To be honest I'm not even sure what information to give about this to get an answer. Any information pointing me in the right direction would be appreciated.

1 个答案:

答案 0 :(得分:0)

So that others like myself can see the answer to this question, Prix and Jonathon Reinhart have provided excellent answers in the comments to my question. But for completeness I'll just sum up.

The load event is fired as soon as the form or user control is ready. The user control is loaded on application start up so that it will be available to use in any form, which is what Prix is talking about. MSDN says this about the load event "Occurs before a form is displayed for the first time." Apparently this can mean even while starting your application, and not necessarily right before being shown.

In other words the load event is not the correct place to put an initialization check.

As for the message box being shown when the solution is loaded in Visual Studio, Jonathon Reinhart provided the key here. Apparently, the visual studio designer interface uses the load event to show the user control in the designer, so the code is executed as the designer loads your user control as well.

Mystification de-mystified. Thank you Prix and Jonathon. Such a simple answer, but I sure was stumped.