Making Your User Controls Available
As you have seen, controls defined in projects in the current solution appear in the Toolbox automatically. According to MSDN (Walkthrough: Automatically Populating the Toolbox with Custom Components
, this is true, but your controls:
- Must implement IComponent
- Must not have the ToolboxItem attribute set to false
- Must not have the DesignTimeVisible attribute set to false
As far as using controls in other
solutions, which is typically where you want them, there are a number of possibilities. With .NET Framework 3.5, the preferred technique is described here: How to: Package and Install Custom Controls for the Toolbox
. However, if you wish to avoid the overhead of packaging your controls for the Visual Studio Content Installer, you can still use older methods from.NET 2.0 and .NET 3.0. For community-packaged controls, see How to: Add Online Controls to the Toolbox
. I could not find a reference in MSDN for a simple installation, but here's the technique.
Simple Toolbox Install
First, right-click on the Toolbox, select "Choose Items
" and you will see the Choose Toolbox Items dialog shown in Figure 11. This includes all the standard components in your Toolbox. To add the controls in your own library, just select the "Browse
" button. That will bring up a standard file dialog filtered on .dll and .exe files. Navigate to your own .dll file and choose Open. Your controls will then be added back to the left dialog in the figure, sorted, and automatically checked to be included. Uncheck any that you do not wish to include, and then press OK to close the dialog. Your controls will now appear in the Toolbox in the General category.
|Figure 11. Adding Controls to the Toolbox: Selecting "Browse…" lets you select a library whose controls are then added to the original dialog box. You may select/unselect any you wish. The final set then populates your Toolbox.
|Author's Note: I would have thought that you could specify an attribute on a control that would add it to a particular category in the Toolbox, but I have not found any way to do this. If you do, please drop me a line. While discussing the Toolbox: Have you ever wondered whether a control exists that accomplishes something you need to do, but you couldn't tell which control it might be? The Windows Forms Programming Guide provides a thorough list of Windows Forms Controls by Function detailing all the common controls, plus a further link to a complete list so you can easily search for what you might need.
Visual Studio also adds a reference to your library to the References for your current project. That is necessary and sufficient for manipulating controls in the visual designer
; however, if you also wish to reference library classes in your code
you need to add an appropriate using
statement, such as using ControlLib;
. The system will know what ControlLib is because of the reference that was just added.
Adding components from a library to your Toolbox is a global action in Visual Studio. That is, if you close your current solution and create a new one, the ClockControl and AlarmClockControl will still be in the Toolbox—but not in your project's references. Visual Studio helps take care of this apparent inconsistency—as soon as you drag a ClockControl from the Toolbox onto your new form, it automatically adds the reference to this new project as well.