Microsoft KB Archive/814736

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

Article ID: 814736

Article Last Modified on 12/6/2006

-

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

-





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.
 * 1) Move two user controls to Panel1.
 * 2) On the Build menu, click Build Solution.
 * 3) 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.



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

By default, UserControl1 is created. Right-click UserControl1, and then click Properties. In the Properties window, set the BackColor property to Highlight .</li> On File menu, click Make Project1.ocx.</li> Click to select a location to save your control file in, and then click OK.</li></ol> </li> To create a Windows application with the user control that you created in step 1, follow these steps: <ol style="list-style-type: lower-alpha;"> 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.</li> On the Tools menu, click Customize Toolbox.

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

By default, Form2 is created</li> Right-click Form2, and then set the IsMdiContainer property to True.</li> From the toolbox, drag a Button control to Form2.</li>  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); } </li> <li>In Solution Explorer, right-click WindowsApplication1, and then click Properties.</li> <li>In the WindowsApplication1 property pages, click to select Form2 in the Startup object list, and then click OK.</li> <li>On the Build menu, click Build Solution.</li> <li>Create a Setup1 folder on your computer.</li> <li>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.</li> <li>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.</li> <li>Copy the Setup1 folder to a computer that runs on the Windows 98 operating system.</li> <li>On the computer that runs on the Windows 98 operating system, click Start, and then click Run.</li> <li>Type cmd in the Open text box, and then press the ENTER key.</li> <li> To register the control, run the following command at the command prompt: regsvr32 <project1.ocx file path on your local computer> </li> <li>Right-click WindowsApplication1, click Open, and then click Button1.

You receive the exception mentioned in the &quot;Symptoms&quot; section of this article.</li></ol> </li></ol>

<div class="references_section">