How to: Inherit from the Control Class

If you want to create a completely custom control to use on a Windows Form, you should inherit from the Control class. While inheriting from the Control class requires that you perform more planning and implementation, it also provides you with 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, which in this case is the actual rendering of the graphical interface of the control, nor does it incorporate any specific user interaction functionality. You must provide all of these aspects through custom code.

To create a custom control

  1. In Visual Studio, create a new Windows Application or Windows Control Library project.

  2. From the Project menu, choose Add Class.

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

    A new custom control is added to your project.

  4. Press F7 to open the Code Editor for your custom control.

  5. Locate the OnPaint method, which will be empty except for a call to the OnPaint method of the base class.

  6. Modify the code to incorporate any custom painting you want for your control.

    For information about 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.

  8. Save and test your control.

See also