Microsoft Knowledge Base Email Alertz

(834615) - Explains why you may receive the TargetInvocationException error message when you try to delete the UserControl1 user control from a Component Designer in a Windows Service project.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 834615 - Last Review: November 27, 2007 - Revision: 3.6

You may receive a "TargetInvocationException" error message when you delete the UserControl1 user control from a Windows Service project in a Visual Basic .NET or Visual Basic 2005 application

On This Page

SYMPTOMS

When you try to delete the UserControl1 user control from Component Designer in a Windows Service project, and the UserControl1 user control contains a Windowless ActiveX control, you may receive the following error message:
The following exception has occurred:
TargetInvocationException: Unable to get the window handle for the 'AxMSFlexGrid' control. Windowless ActiveX controls are not supported.

CAUSE

The UserControl1 user control that contains the Windowless ActiveX controls is not currently supported in a Windows Service project.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce the problem

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. Create a new Microsoft Visual Basic .NET or Microsoft Viusal Basic 2005 Windows Service project that is named WindowsService1. By default, the project is created under the solution that is named WindowsService1.
  3. Add a Windows Control Library project that is named WindowsControlLibrary1 to the WindowsService1 solution.
  4. Open the Customize Toolbox dialog box.

    To do this in Visual Studio .NET 2002, right-click Components under Toolbox, and then click Customize Toolbox.

    To do this in Visual Studio .NET 2003, right-click Components under Toolbox, and then click Add/Remove Items.

    To do this in Visual Studio 2005, right-click Components under Toolbox, and then click Choose Items.
  5. In the Customize Toolbox dialog box, click to select the Microsoft FlexGrid Control, version 6.0 check box on the COM Components tab, and then click OK.
  6. On the UserControl1.vb[Design] page, click the Toolbox, click the Components tab, and then click Microsoft FlexGrid Control, version 6.0.
  7. On the Build menu, click Build Solution.

    The Windowscontrollibrary1.dll file is created.
  8. In Solution Explorer, double-click the Service1.vb file under the WindowsService1 project.
  9. Add the UserControl1 user control to the Service1.vb[Design] page.

    If you are using Visual Studio .NET 2002, follow these steps:
    1. Under Toolbox, click Components, right-click Components, and then click Customize Toolbox. The Customize Toolbox dialog box appears.
    2. In the Customize Toolbox dialog box, click the .NET Framework Components tab, and then click Browse to locate the WindowsControlLibrary1.dll file that you created in step 7.
    3. Click the WindowsControlLibrary1.dll file, and then click Open.
    4. Click OK to close the Customize Toolbox dialog box.
    5. From the Components tab of the Toolbox, drag the UserControl1 user control to the Service1.vb[Design] page.

      Note If you receive the following error message, click OK to close the error message dialog box:
      --------------------------- Microsoft Development Environment --------------------------- The .NET assembly 'Interop.MSFlexGridLib' could not be found. --------------------------- OK ---------------------------
    6. From the Components tab of the Toolbox, drag the UserControl1 user control to the Service1.vb[Design] page.
    If you are using Visual Studio .NET 2003, click the My User Controls tab on the Toolbox, and then click UserControl1.
  10. Try to delete the UserControl1 user control from the Service1.vb[Design] page.

    You may receive the error message that is mentioned in the "Symptoms" section.

REFERENCES

For additional information, visit the following Microsoft Developer Network (MSDN) Web sites:
Windows Control Library template
http://msdn2.microsoft.com/en-us/library/kxczf775(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/kxczf775(vs.71).aspx)

Windows Service applications
http://msdn2.microsoft.com/en-us/library/aa984074(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/aa984074(VS.71).aspx)

Using ActiveX Controls with Windows Forms in Visual Studio .NET
http://msdn2.microsoft.com/en-us/library/ms973200.aspx (http://msdn2.microsoft.com/en-us/library/ms973200.aspx)

APPLIES TO
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic 2005
Keywords: 
kbvs2005swept kbvs2005applies kbtoolbar kbservice kbmsg kbdll kbcontrol kbuser kbprb KB834615
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Anonymous User Report As Irrelevant  
Written: 8/30/2004 7:17 AM
This is in referance to the ERROR: TargetInvocationException: Unable to get the window handle for the 'AxMSFlexGrid' control. Windowless ActiveX controls are not supported. I was gettin this error in my application andi understand it occured if i tried to close the application without showing the usercontrol that had a Mask Edit COntrol. The cause of error is the dispose method of the usercontrol that gets called. A work around this problem is to not allow the user control to dispose. as in comment out the //base.Dispose( disposing ); The component.dispose occure anyways and hence will not lead to any memory leaks either. Hope that was helpful Regards,

Tom McAnnally Report As Irrelevant  
Written: 5/18/2007 8:27 AM
This method will allow the dispose, but avoid the TargetInvocationException. Here is a better approach which works and doesn't require any tab cycling hacks. What you want to do is avoid calling OnVisibleChanged after DestroyHandle since .NET is about to send a window message to a ActiveX control without a Windows Handle. That is what causes the System.Reflection.TargetInvocationException. Private x As Boolean Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call x = False End Sub 'UserControl1 overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Protected Overrides Sub DestroyHandle() Debug.WriteLine("Crash coming, get ready to avoid it!") x = True MyBase.DestroyHandle() End Sub Protected Overrides Sub OnVisibleChanged(ByVal e As EventArgs) If Not x Then Debug.WriteLine("Call Base Class") MyBase.OnVisibleChanged(e) Else Debug.WriteLine("Don't bother crashing") End If End Sub