This documentation is archived and is not being maintained.

Authoring User Controls

User controls can be employed in many ways. You can author them as part of a Windows desktop application project, and use them only on forms in the project. Or you can author them in a Windows Control Library project, compile the project into an assembly, and use the controls in other projects. You can even inherit from them, and use visual inheritance to customize them quickly for special purposes.

Note   If you want to author a user control to use on Web Forms, see Developing ASP.NET Server Controls.

To author a user control

  1. Open a new Windows Application project.
  2. On the Project menu, click Add User Control.
  3. Add controls from the Toolbox to the user control surface.
  4. Place code in event procedures, to handle events raised by the UserControl or by its constituent controls.
  5. Close the designer for the user control, and save the file.
  6. Give the class file (.vb or .cs file) the name you want the user control to have.
  7. On the Build menu, click Build.

    The project must be built in order for user controls to appear in the Toolbox.

  8. Use the Windows Forms tab of the Toolbox to add instances of your control to Form1.

To author a control class library

  1. Open a new Windows Control Library project.

    By default, the project contains a user control.

    Note   The Windows Control Library template is not available in the Standard Edition of Visual Basic and Visual C# .NET. For more information, see Visual Basic Standard Edition Features or Visual C# Standard Edition Features.
  2. Add controls and code as described in the procedure above.
  3. Choose a control you do not want inheriting classes to change, and set the Modifiers property of this control to Private.
  4. Build the DLL.

To inherit from a user control in a control class library

  1. Open a Windows Application project.
  2. In Solution Explorer, right-click the References folder and choose Add Reference to open the Add Reference dialog box.
  3. Select the Projects tab and double-click your DLL.
  4. On the Project menu, click Add Inherited Control.
  5. In the Inheritance Picker dialog box, double-click the control you want to inherit from.
  6. In the Add New Item dialog box, click Open (Inherited User Control is already selected).
  7. In the Inheritance Picker dialog box, browse to and double-click the control you want to inherit from.

    A new control is added to your project.

  8. Open the visual designer for the new control and add additional constituent controls.

    You can see the constituent controls that were inherited from the user control in your DLL, and you can alter the properties of controls whose Modifiers property is Public. You cannot change the properties of the control whose Modifiers property is Private.

See Also

Walkthrough: Authoring a User Control With Visual Basic .NET | Walkthrough: Inheriting from a Windows Forms Control with Visual Basic .NET | Control Type Recommendations | Authoring Controls for Windows Forms

Show: