This documentation is archived and is not being maintained.

Inheriting from Existing Windows Forms Controls

If you want to extend the functionality of an existing control, you can create a control derived from an existing control through inheritance. When inheriting from an existing control, you inherit all of the functionality and visual properties of that control. For instance, if you were creating a control that inherited from Button, your new control would look and act exactly like a standard Button control. You could then extend or modify the functionality of your new control through the implementation of custom methods and properties. In some controls, you can also change the visual appearance of your inherited control by overriding its OnPaint method.

To create an inherited control

  1. Create a new Windows project.

    This project can be of any type, such as a Windows Application project or a Windows Control Library project. If you choose a Windows Control Library, you may use the blank control provided and skip steps 2 and 3.

  2. From the Project menu, choose Add Inherited Control.

    The Add New Item dialog box appears.

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

    A new custom control is added to your project.

  4. In the Code Editor, locate the line that specifies Control as the base class to inherit from. Change the name of the base class to the name of the control from which you want to inherit.

    For example, if you want to inherit from Button, the line would read:

    ' Visual Basic
    Inherits System.Windows.Forms.Button
    
    // C# 
    public class CustomControl1 : System.Windows.Forms.Button
    
  5. Implement any custom methods or properties that your control will incorporate. For further information see Properties, Methods, and Events for Custom Controls.
  6. If you want to modify the graphical appearance of your control, override the OnPaint method.
    Note   Overriding OnPaint will not allow you to modify the appearance of all controls. Those controls that have all of their painting done by Windows (for example, Textbox) never call their OnPaint method, and thus will never use the custom code. Please refer to the documentation of the particular control you wish to modify to see if the OnPaint method is available. If a control does not have OnPaint listed as a member method, then you cannot alter its appearance by overriding this method. For more information on custom painting, see Custom Control Painting and Rendering.
    ' Visual Basic
    Protected Overrides Sub OnPaint(ByVal pe As _
       System.Windows.Forms.PaintEventArgs)
       MyBase.OnPaint(pe)
       ' Insert code to do custom painting. If you want to completely
       ' change the appearance of your control, do not call
       ' MyBase.OnPaint(pe).
    End Sub
    
    // C#
    protected override void OnPaint(PaintEventArgs pe)
    {
       // Do not call base.OnPaint if you want to completely
       // control the appearance of the control.
       base.OnPaint(pe);
       // Insert code to do custom painting.
    }
    
  7. Save and test your control.

See Also

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

Show: