Inheriting from the UserControl Class
If you want to combine the functionality of one or more Windows Forms controls with custom code, then you should create a user control. User controls combine rapid control development with standard Windows Forms control functionality and the versatility created by adding custom properties and methods. When creating a user control, you are presented with a visible designer, upon which you may place standard Windows Forms controls. These controls retain all of their inherent functionality, as well as the look and feel of standard controls, but once they are built into the user control they are no longer available to the developer through code. The user control does its own painting and also handles all of the basic functionality associated with controls.
To create a user control
- Create a new Windows Control Library project.
A new project is created with a blank user control.
- Drag controls from the Windows Forms tab of the Toolbox onto your designer.
- These controls should be positioned and designed as you want them to appear in the final user control. If you want to allow developers to access the constituent controls, you must declare them as public, or selectively expose properties of the constituent control. For details, see Exposing Properties of Constituent Controls.
- Implement any custom methods or properties that your control will incorporate. For details, see Properties, Methods, and Events for Custom Controls.
- Save and test your control.