Microsoft KB Archive/318386

= FIX: The Load event does not execute when you use the ShowDialog method to show a form that contains an ActiveX control =

Article ID: 318386

Article Last Modified on 1/19/2006

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Professional Edition

-



This article was previously published under Q318386



SYMPTOMS
When you show a form by using the ShowDialog method, the Load event of that form does not execute.



RESOLUTION
The following are several potential workarounds or options that you can use to resolve this problem:
 * Override the window procedure (WndProc) of the form and manually call the Load event procedure.
 * Use a non-ActiveX control whenever possible. For example, rather than use the Microsoft Windows Common Dialog control, as in the following sample, use the equivalent controls that are provided with Visual Studio .NET, such as OpenFileDialog, SaveFileDialog, or PrintDialog.
 * Use the Show method instead of the ShowDialog method.
 * Determine if you can use an event procedure other than the Load event procedure in your application.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Visual Studio .NET 2003.



MORE INFORMATION
This problem occurs when you show a form under the following two conditions:
 * 1) When you show the form modally by using the ShowDialog method.
 * 2) When the form is host to an ActiveX control.

NOTE: The following steps demonstrate this problem by using Visual Basic .NET. However, this behavior occurs at the Windows Forms level, so the behavior occurs regardless of the programming language that you use.

Steps to reproduce the behavior
NOTE: To perform the steps in the &quot;Sample Resolution&quot; section, you must first perform the steps in this section because the example in &quot;Sample Resolution&quot; uses the sample application that you create in this section.

To create a sample application that reproduces the behavior, follow these steps:  Create a new Visual Basic .NET Windows application. Form1 is created by default. Add a command button to Form1.  Paste the following sample code in the Click event procedure for the button: Dim f As New Form2

f.ShowDialog  On the Project menu, click Add Windows Form and follow the onscreen instructions to add Form2 to the project. On the Tools menu, click Customize Toolbox to add a reference to an ActiveX control, such as Microsoft Windows Common Dialog.

NOTE: This example uses Microsoft Windows Common Dialog. However, you can use any ActiveX control to reproduce the behavior. Place the referenced control on Form2.</li>  Paste the following sample code in the Load event procedure of Form2: MessageBox.Show(&quot;Form2 Load Event&quot;) </li> Run the sample and click the button on Form1. Form2 appears but the message box from the Load event procedure in Form2 does not appear.</li></ol>

Sample resolution
To override the window procedure of the form, you must specifically look for the WM_SHOWWINDOW message, and then manually call the method that you want. To override WNDPROC, follow these steps: <ol>  Paste the following sample code in the Form2 class of the application that you created in &quot;Steps to Reproduce Behavior&quot;: Private Const WM_SHOWWINDOW = &H18 Private m_loaded As Boolean

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If m.Msg = WM_SHOWWINDOW Then If Not m_loaded Then MyBase.OnLoad(New System.EventArgs) End If  End If   MyBase.WndProc(m) End Sub </li>  Add the following sample code to the Load event procedure of Form2: m_loaded = True </li> Run the sample, and then click the button on Form1. Form2 appears and the message box appears, which indicates that the Load event procedure executes.

NOTE: The sample code adds the Boolean variable, m_loaded, which prevents the Load event procedure from running twice, in case the form is shown by using the Show method instead of by using the ShowDialog method.</li></ol>

Keywords: kbtshoot kbbug kbfix kbvs2002sp1sweep kbvs2005swept kbvs2005doesnotapply KB318386

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.