|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Troubleshooting Control and Component Authoring
This topic lists the following common problems that arise when developing components and controls. For more information, see Programming with Components.
Cannot Add Control to Toolbox
Cannot Debug the Windows Forms User Control or Component
Event Is Raised Twice in Inherited Control or Component
Design-Time Error: "Failed to Create Component 'Component Name'"
Component Icon Does Not Appear in Toolbox
If you want to add a custom control that you created in another project or a third-party control to the Toolbox, you must do so manually. If the current project contains your control or component, it should appear in the Toolbox automatically. For more information, see Walkthrough: Automatically Populating the Toolbox with Custom Components.
To add a control to the Toolbox
Right-click the Toolbox and from the shortcut menu, select Choose Items.
In the Choose Toolbox Items dialog box, add the component:
If you want to add a .NET Framework component or control, click the .NET Framework Components tab.
– or –
If you want to add a COM component or ActiveX control, click the COM Components tab.
If your control is listed in the dialog box, confirm it is selected, and then click OK.
The control is added to the Toolbox.
If your control is not listed in the dialog box, do the following:
Click the Browse button.
Browse to the folder that contains the .dll file that contains your control.
Select the .dll file and click Open.
Your control appears in the dialog box.
Confirm that your control is selected, and then click OK.
Your control is added to the Toolbox.
If your control derives from the UserControl class, you can debug its run-time behavior with the test container. For more information, see How to: Test the Run-Time Behavior of a UserControl.
Other custom controls and components are not stand-alone projects. They must be hosted by an application such as a Windows Forms project. To debug a control or component, you must add it to a Windows Forms project.
To debug a control or component
From the Build menu, click Build Solution to build your solution.
From the File menu, choose Add, and then New Project to add a test project to your application.
In the Add New Project dialog box choose Windows Application for the type of project.
In Solution Explorer, right-click the References node for the new project. On the shortcut menu, click Add Reference to add a reference to the project containing the control or component.
Create an instance of your control or component in the test project. If your component is in the Toolbox, you can drag it to your designer surface, or you can create the instance programmatically, as shown in the following code example.
You can now debug your control or component as usual.
For more information about debugging, see Debugging in Visual Studio and Walkthrough: Debugging Custom Windows Forms Controls at Design Time.
This is likely due to a duplicated Handles clause. For more information, see Troubleshooting Inherited Event Handlers in Visual Basic.
The STAThreadAttribute informs the common language runtime (CLR) that Windows Forms uses the single-threaded apartment model. You may notice unintended behavior if you do not apply this attribute to your Windows Forms application's Main method. For example, background images may not appear for controls like ListView. Some controls may also require this attribute for correct AutoComplete and drag-and-drop behavior.
When you use ToolboxBitmapAttribute to associate an icon with your custom component, the bitmap does not appear in the Toolbox for autogenerated components. To see the bitmap, reload the control by using the Choose Toolbox Items dialog box. For more information, see How to: Provide a Toolbox Bitmap for a Control.