This documentation is archived and is not being maintained.

Inheriting from the Control Class

If you want to create a completely custom control to use on a Windows Form, the class to inherit from is Control. While inheriting from the Control class requires greater planning and implementation, it also makes available the largest range of options. When inheriting from Control, you inherit the very basic functionality that makes controls work. The functionality inherent in the Control class handles user input through the keyboard and mouse, defines the bounds and size of the control, provides a windows handle, and provides message handling and security. It does not incorporate any painting (the actual rendering of the graphical interface of the control), nor does it incorporate any specific user interaction functionality. The author must provide all of these aspects through custom code.

To create a custom control

  1. Create a new Windows Application or Windows Control Library project.
    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. From the Project menu, choose Add Inherited Control.

    The Add New Item dialog box appears.

  3. In the Add New Item dialog box, click Custom Control.

    A new custom control is added to your project.

  4. Open the Code Editor for your CustomControl.
  5. Locate the OnPaint method, which will be empty except for a call to the OnPaint of the base class.
  6. Modify the code to incorporate any custom painting you want for your control.

    For details on writing code to render graphics for controls, see Custom Control Painting and Rendering.

  7. Implement any custom methods, properties, or events that your control will incorporate. For more information, see Properties, Methods, and Events for Custom Controls.
  8. Save and test your control.

See Also

Inheriting from the UserControl Class | Inheriting from Existing Windows Forms Controls | Authoring Controls for Windows Forms | Troubleshooting Inherited Event Handlers in Visual Basic .NET