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



The information in this article 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 "Sample Resolution" section, you must first perform the steps in this section because the example in "Sample Resolution" uses the sample application that you create in this section.

To create a sample application that reproduces the behavior, follow these steps:
  1. Create a new Visual Basic .NET Windows application. Form1 is created by default.
  2. Add a command button to Form1.
  3. Paste the following sample code in the Click event procedure for the button:
    Dim f As New Form2()
    
    f.ShowDialog()
    					
  4. On the Project menu, click Add Windows Form and follow the onscreen instructions to add Form2 to the project.
  5. 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.
  6. Place the referenced control on Form2.
  7. Paste the following sample code in the Load event procedure of Form2:
    MessageBox.Show("Form2 Load Event")
    					
  8. 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.

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:
  1. Paste the following sample code in the Form2 class of the application that you created in "Steps to Reproduce Behavior":
    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
    					
  2. Add the following sample code to the Load event procedure of Form2:
    m_loaded = True
    					
  3. 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.

Modification Type:MinorLast Reviewed:1/19/2006
Keywords:kbvs2005doesnotapply kbvs2005swept kbtshoot kbvs2002sp1sweep kbfix kbbug KB318386 kbAudDeveloper