BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Visual Studio
VS 2003+

Add Custom Controls to the Visual Studio Toolbox

The Visual Studio toolbox provides a simple, categorised list of the controls and components that Visual Studio knows how to add to your projects. When you create your own custom Windows Forms controls, it can be useful to include them in the toolbox.

The Visual Studio Toolbox

Visual Studio provides the Toolbox. This collapsible area of the screen provides access to a set of controls, components, data objects and other items, organised in useful groups. These items can then be easily incorporated into your projects using various designers. For example, several groups provide lists of Windows Forms controls that can be added to forms with simple mouse gestures.

Visual Studio Toolbox

When you add an item from the toolbox to your project, various activities take place in the background without requiring further manual actions. These ensure that your code continues to operate as expected. For example, when you add a control from the toolbox to a Windows Form design surface, the DLL containing the control is added as a reference in your project.

When you create your own custom controls and components they are not included in the toolbox automatically. You can, however, add your controls to a section of the toolbox by following a simple process. The same steps can also be used to add existing items that are not included in the toolbox by default.

A Simple Custom Control

To demonstrate the process for adding a new control to the toolbox, we first need a control to work with. This is not the main focus of the article so the control will be simplistic. In this case, we will create a variation upon the Windows Forms TextBox control, which will automatically select all of its text when it receives the focus.

Creating the Custom Control Project

Start by creating a new C# class library project named "AutoSelectTextBoxControl". As we will be modifying the existing TextBox control by subclassing it, add a reference to System.Windows.Forms to the project.

To create the class for the new control, rename the automatically generated class file to "AutoSelectTextBox.cs". Open the file and add the following using directive at the top of the file. This will allow us access to the existing TextBox class without fully qualifying its name.

using System.Windows.Forms;

We can now complete the control by removing the existing class declaration and replacing it with the code below. The new class inherits all of the functionality of the TextBox class. It overrides the OnEnter method, which is called whenever the control receives the focus. The new version of the OnEnter method calls the base method before selecting all of the text in the TextBox.

public class AutoSelectTextBox : TextBox
{
    protected override void OnEnter(EventArgs e)
    {
        base.OnEnter(e);
        SelectAll();
    }
}

Now that the simple control is complete, save the project, compile it in Release mode and close the project.

Adding the Control to the Toolbox

In this section we will add the control created above to the toolbox. To begin, create a new Windows Forms project and ensure that you are viewing a form designer window. The toolbox should be visible and should be showing various sections including those that relate to designing forms. If the toolbox is not visible, open the View menu and select the "Toolbox" option. If the toolbox is still not visible, it may be collapsed to a tab at the side of the screen. If so, click the tab and click the pushpin icon to lock the toolbox in place.

New toolbox items are added to the currently selected section by default, although they can be moved later by simple dragging and dropping. Select the section to which you would like to add the new control by clicking it. You can then begin the process of adding the new toolbox item by right-clicking the toolbox section and selecting "Choose Items" from the context-sensitive menu that appears. You can also select "Choose Toolbox Items" from the Tools menu. NB: When using Visual Studio 2003, the menu shows "Add/Remove Items".

When you select the appropriate menu option, the Choose Toolbox Items dialog box is displayed. There may be a brief delay whilst the dialog box is prepared.

Visual Studio Choose Toolbox Items Dialog Box

The dialog box shows several lists of items that can be included in the toolbox. Each item includes a checkbox. If the checkbox is ticked, the item is already included in the toolbox. Custom controls are not included in the lists by default so must be located manually.

To add the custom TextBox control, click the Browse button. Use the file browser that appears to locate the AutoSelectTextBoxControl.dll that you compiled earlier. Select this DLL and click the Open button to return to the Choose Toolbox Items dialog box. The AutoSelectTextBox control should now appear in the list. Ensure that this item's checkbox is ticked and then click the OK button. The new control will now be visible in the toolbox.

To use the toolbox item, simply drag it onto the form designer. A new AutoSelectTextBox control will be placed on the form. Note also that a reference to the underlying AutoSelectTextBoxControl.dll is added to the project automatically.

29 March 2009