Creating Custom .NET Controls with C#by Budi Kurniawan
Windows programmers have a wide variety of controls to choose from in the
System.Windows.Forms namespace in .NET's Framework class library. You have controls as simple as
CheckBox, as well as controls as sophisticated as the
ColorDialog controls. These Windows controls are more than enough for most applications; however, sometimes you need controls that are not available from the standard library. In these circumstances, you have to roll up your sleeves and write
your own. This article shows you how to develop a custom control with C# and presents a simple custom control.
Before you start writing the first line of code for your custom control, you should familiarize yourself with two classes in the
Control class is important because it is the parent class of Windows visual components. Your custom class will be a descendent of the
Control class as well. Your custom controls, however, don't normally inherit directly from the
Control class. Instead, you extend the
UserControl class. The first two sections of this article discuss these two classes. In the final section, you'll build your own custom control, the
The Control Class
Control class provides very basic functionality required by classes that display information to the Windows application user. This class handles user input through the keyboard and the mouse, as well as message routing and security. More importantly, the
Control class defines the bounds of a control (its position and size), although it does not implement painting.
Windows forms controls use ambient properties, so child controls can appear like their surrounding environment. In this context, "ambient" means that the property is, by default, retrieved from the parent control. If the control does not have a parent and the property is not set, the control tries to determine the value of the ambient property through the
Site property. If the control is not sited, if the site does not support ambient properties, or if the property is not set on the
AmbientProperties object, the control uses its own default values. Typically, an ambient property represents a characteristic of a control, such as
BackColor, that is communicated to a child control. For example, by default a button will have the same
BackColor as its parent form.
A number of the
Control class's properties, methods, and events are carried through by its child classes without any change.
The Control Class's Properties
The following are some of the
Control class's most important properties:
- The background color of the control, represented by a
System.Drawing.Colorobject. You can programmatically assign a
System.Drawing.Colorobject to this property using code like this:
control.BackColor = System.Drawing.Color.Red
- A Boolean that indicates whether or not the control is enabled. The default value is
- The position of the top-left corner of the control in its container, represented by a
- The name of the control.
- Returns a reference to the container or parent of the control. For example, the parent of a control that is added to a form is the form itself; if we add
Button1to a form, we can change the title of that form to "Thank you":
Button1.Parent.Text = "Thank you"
- The size of the control, as represented by a
- The string that is associated with the control. For example, in a label control, the text property is the string that appears on the label body.
The Methods of the Control Class
Some of the frequently used methods of the
Control class are:
- Shows the entire control, in cases where some other control is overlaying it.
- Obtains the
System.Drawing.Graphicsobject of the control, on which you can draw using the various methods of the
System.Drawing.Graphicsclass. For instance, the following code obtains the
Graphicsobject of a button control called
Button1, and then draws a diagonal green line across the button's body:
Imports System.Drawing Dim graphics As Graphics = Button1.CreateGraphics Dim pen As Pen = New Pen(Color.Green) graphics.DrawLine(pen, 0, 0, _ Button1.Size.Width, Button1.Size.Height)
Drawing on a control this way, however, does not result in "permanent" drawings. When the control is repainted, as it is when the form containing the control is resized, the graphics will disappear. The section "The RoundButton Control" below explains how to make the user interface redraw every time the control is repainted.
- Gives the focus to the control, making it the active control.
- Set the control's
False, so that it is not shown.
- Returns the next control in the tab order.
- Raises the Event event; possible events include
TextChanged. For example, calling the
OnClickmethod of the control will trigger its
- Sets the control's
True, so that the control is shown.
The UserControl ClassThe
UserControlclass provides an empty control that can be used to create other controls. It is an indirect child of the
Controlclass. The object hierarchy of this control is as follows.
System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control System.Windows.Forms.ScrollableControl System.Windows.Forms.ContainerControl System.Windows.Forms.UserControl
UserControl class inherits all of the standard positioning and mnemonic-handling code from the
ContainerControl class. This code is needed in a user control.
Pages: 1, 2