Custom controls (Windows Forms .NET)

With Windows Forms, you can create new controls or modify existing controls through inheritance. This article highlights the differences among the ways of creating new controls, and provides you with information about how to choose a particular type of control for your project.

Important

The Desktop Guide documentation for .NET 7 and .NET 6 is under construction.

Base control class

The Control class is the base class for Windows Forms controls. It provides the infrastructure required for visual display in Windows Forms applications and provides the following capabilities:

  • Exposes a window handle.
  • Manages message routing.
  • Provides mouse and keyboard events, and many other user interface events.
  • Provides advanced layout features.
  • Contains many properties specific to visual display, such as ForeColor, BackColor, Height, and Width.

Because so much of the infrastructure is provided by the base class, it's relatively easy to develop your own Windows Forms controls.

Create your own control

There are three types of custom controls you can create: user controls, extended controls, and custom controls. The following table helps you decide which type of control you should create:

If ... Create a ...
  • You want to combine the functionality of several Windows Forms controls into a single reusable unit.
Design a user control by inheriting from System.Windows.Forms.UserControl.
  • Most of the functionality you need is already identical to an existing Windows Forms control.
  • You don't need a custom graphical user interface, or you want to design a new graphical user interface for an existing control.
Extend a control by inheriting from a specific Windows Forms control.
  • You want to provide a custom graphical representation of your control.
  • You need to implement custom functionality that isn't available through standard controls.
Create a custom control by inheriting from System.Windows.Forms.Control.

User controls

A user control is a collection of Windows Forms controls presented as a single control to the consumer. This kind of control is referred to as a composite control. The contained controls are called constituent controls.

A user control holds all of the inherent functionality associated with each of the contained Windows Forms controls and enables you to selectively expose and bind their properties. A user control also provides a great deal of default keyboard handling functionality with no extra development effort on your part.

For example, a user control could be built to display customer address data from a database. This control would include a DataGridView control to display the database fields, a BindingSource to handle binding to a data source, and a BindingNavigator control to move through the records. You could selectively expose data binding properties, and you could package and reuse the entire control from application to application.

For more information, see User control overview.

Extended controls

You can derive an inherited control from any existing Windows Forms control. With this approach, you can keep all of the inherent functionality of a Windows Forms control, and then extend that functionality by adding custom properties, methods, or other features. With this option, you can override the base control's paint logic, and then extend its user interface by changing its appearance.

For example, you can create a control derived from the Button control that tracks how many times a user has clicked it.

In some controls, you can also add a custom appearance to the graphical user interface of your control by overriding the OnPaint method of the base class. For an extended button that tracks clicks, you can override the OnPaint method to call the base implementation of OnPaint, and then draw the click count in one corner of the Button control's client area.

Custom controls

Another way to create a control is to create one substantially from the beginning by inheriting from Control. The Control class provides all of the basic functionality required by controls, including mouse and keyboard handling events, but no control-specific functionality or graphical interface.

Creating a control by inheriting from the Control class requires more thought and effort than inheriting from UserControl or an existing Windows Forms control. Because a great deal of implementation is left for you, your control can have greater flexibility than a composite or extended control, and you can tailor your control to suit your exact needs.

To implement a custom control, you must write code for the OnPaint event of the control, which controls how the control is visually drawn. You must also write any feature-specific behaviors for your control. You can also override the WndProc method and handle windows messages directly. This is the most powerful way to create a control, but to use this technique effectively, you need to be familiar with the Microsoft Win32® API.

An example of a custom control is a clock control that duplicates the appearance and behavior of an analog clock. Custom painting is invoked to cause the hands of the clock to move in response to Tick events from an internal Timer component.

Custom design experience

If you need to implement a custom design-time experience, you can author your own designer. For composite controls, derive your custom designer class from the ParentControlDesigner or the DocumentDesigner classes. For extended and custom controls, derive your custom designer class from the ControlDesigner class.

Use the DesignerAttribute to associate your control with your designer.

The following information is out of date but may help you.