You receive an "Unable to get the window handle" exception when you display a child form that contains ActiveX user controls

Article translations Article translations
Article ID: 814736 - View products that this article applies to.
Expand all | Collapse all

On This Page

SYMPTOMS

You create a Microsoft Windows form (such as Form1), and then you set the IsMdiContainer property of the form to True. You create a Microsoft Visual Basic 6.0 user control, and then you add the user control to a Windows form (Form1) in Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005. If you create the Windows form as the parent form of another Windows form that contains user controls, you display the child form, and then you run the application on a computer that runs on the Microsoft Windows 98 operating system, you receive the following error message:
System.InvalidOperationException: Unable to get the window handle for the 'AxUserControl1' control. Windowless ActiveX controls are not supported.

Note This article assumes that Visual Basic 6.0 and Microsoft .NET Framework 1.0 are installed on your computer with Windows 98.

CAUSE

When you create a Windows form that contains user controls as a multiple-document interface (MDI) child form, the Windows form always destroys the child form, and then re-creates the child form. The Windows form destroys all the controls on the child form, and then re-creates the controls. However, the first user control is destroyed, but it is not re-created. The second user control is not destroyed or re-created. Therefore, you receive the error when you display the child form, because the first user control hwnd is NULL.

WORKAROUND

To work around this problem, put the user controls in a Panel control. To do this, follow these steps:
  1. From the toolbox, drag a Panel control to Form1.

    By default, Panel1 is created.
  2. Move two user controls to Panel1.
  3. On the Build menu, click Build Solution.
  4. In the Bin subfolder of the WindowsApplication1 project folder, copy the WindowsApplication1.exe file, and then paste the file in the Setup1 folder on the computer that runs on Windows 98.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce the Behavior


  1. To create a Microsoft Visual Basic ActiveX control, follow these steps:
    1. Open Visual Basic 6.0.
    2. In the New Project dialog box, click ActiveX Control, and then click Open.

      By default, UserControl1 is created.
    3. Right-click UserControl1, and then click Properties.
    4. In the Properties window, set the BackColor property to Highlight.
    5. On File menu, click Make Project1.ocx.
    6. Click to select a location to save your control file in, and then click OK.
  2. To create a Windows application with the user control that you created in step 1, follow these steps:
    1. In Visual Studio .NET or in Visual Studio 2005, start a new Windows application by using Microsoft Visual Basic .NET, Microsoft Visual Basic 2005, or Microsoft Visual C# .NET.

      By default, Form1 is created.
    2. On the Tools menu, click Customize Toolbox.

      Notes
      • In Microsoft Visual Studio .NET 2003, click Add/Remove Toolbox Items on the Tools menu.
      • In Microsoft Visual Studio 2005, click Choose Toolbox Items on the Tools menu.
    3. Click the COM Components tab, and then click Browse.
    4. Locate the Project1.ocx file that you created in step 1, click Project1.ocx, and then click Open.
    5. In the Customize Toolbox dialog box, click OK.
    6. From the toolbox, drag UserControl1 to Form1.
    7. Repeat step 2f to add another user control to the form.
    8. On the Project menu, click Add Windows Form, and then click Open.

      By default, Form2 is created
    9. Right-click Form2, and then set the IsMdiContainer property to True.
    10. From the toolbox, drag a Button control to Form2.
    11. Add the following code to the Button1_Click event handler:

      Visual Basic .NET or Visual Basic 2005 Code
      Dim b As Form1
      b = New Form1()
      'Set Form2 as the parent of Form1.
      b.MdiParent = Me
      'Display Form1 when the button is clicked.
      b.Show()
      Visual C# .NET Code
      Form1 b= new Form1();
      //Set Form2 as the parent of Form1.
      b.MdiParent=this;
      //Display Form1 when the button is clicked.
      b.Show();
      Note Add the following code after the Button1_Click event handler in Visual C# .NET:
      [STAThread]
      static void Main() 
      {
           Application.Run(new Form2());
      }
    12. In Solution Explorer, right-click WindowsApplication1, and then click Properties.
    13. In the WindowsApplication1 property pages, click to select Form2 in the Startup object list, and then click OK.
    14. On the Build menu, click Build Solution.
    15. Create a Setup1 folder on your computer.
    16. From the Bin subfolder in your WindowsApplication1 project folder, copy the AxInterop.Project1.dll file, copy the Interop.Project1.dll file, and copy the WindowsApplication1.exe file to the Setup folder.
    17. From the location you where you saved the Visual Basic 6.0 user control that you created, copy the Project1.ocx file to the Setup1 folder.
    18. Copy the Setup1 folder to a computer that runs on the Windows 98 operating system.
    19. On the computer that runs on the Windows 98 operating system, click Start, and then click Run.
    20. Type cmd in the Open text box, and then press the ENTER key.
    21. To register the control, run the following command at the command prompt:
      regsvr32 <project1.ocx file path on your local computer>
    22. Right-click WindowsApplication1, click Open, and then click Button1.

      You receive the exception mentioned in the "Symptoms" section of this article.

REFERENCES

For more information, visit the following MSDN Web site:
http://msdn2.microsoft.com/en-us/library/wtzawcsz(vs.71).aspx

Properties

Article ID: 814736 - Last Review: December 6, 2006 - Revision: 2.7
APPLIES TO
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual Basic 4.0 Enterprise Edition
  • Microsoft Windows 98 Standard Edition
Keywords: 
kbvs2005swept kbvs2005applies kbhotfixserver kbqfe kbsetup kbdeployment kbwindowsforms kbmdi kbctrl kbcontrol kbprb KB814736

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com