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
- Create a new Windows Application or Windows Control Library project.
- From the Project menu, choose Add Inherited Control.
The Add New Item dialog box appears.
- In the Add New Item dialog box, click Custom Control.
A new custom control is added to your project.
- Open the Code Editor for your CustomControl.
- Locate the OnPaint method, which will be empty except for a call to the OnPaint of the base class.
- 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.
- Implement any custom methods, properties, or events that your control will incorporate. For more information, see Properties, Methods, and Events for Custom Controls.
- Save and test your control.