Creating Custom .NET Controls with C#
by Budi Kurniawan03/18/2002
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 Label, TextBox, and CheckBox, as well as controls as sophisticated as the MonthCalendar and 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 System.Windows.Forms namespace: Control and UserControl. 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 RoundButton control.
The Control Class
The 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:
- BackColor
- The background color of the control, represented by a
System.Drawing.Colorobject. You can programmatically assign aSystem.Drawing.Colorobject to this property using code like this:control.BackColor = System.Drawing.Color.Red - Enabled
- A Boolean that indicates whether or not the control is enabled. The default value is
True. - Location
- The position of the top-left corner of the control in its container, represented by a
System.Drawing.Pointobject. - Name
- The name of the control.
- Parent
- 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" - Size
- The size of the control, as represented by a
System.Drawing.Sizeobject. - Text
- 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:
- BringToFront
- Shows the entire control, in cases where some other control is overlaying it.
- CreateGraphics
- Obtains the
System.Drawing.Graphicsobject of the control, on which you can draw using the various methods of theSystem.Drawing.Graphicsclass. For instance, the following code obtains theGraphicsobject of a button control calledButton1, 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.
- Focus
- Gives the focus to the control, making it the active control.
- Hide
- Set the control's
Visibleproperty toFalse, so that it is not shown. - GetNextControl
- Returns the next control in the tab order.
- OnEvent
- Raises the Event event; possible events include
Click,ControlAdded,ControlRemoved,DoubleClick,DragDrop,DragEnter,DragLeave,DragOver,Enter,GotFocus,KeyDown,KeyPress,KeyUp,LostFocus,MouseDown,MouseEnter,MouseHover,MouseLeave,MouseMove,MouseUp,Move,Paint,Resize, andTextChanged. For example, calling theOnClickmethod of the control will trigger itsClickevent. - Show
- Sets the control's
Visibleproperty toTrue, so that the control is shown.
The UserControl Class
TheUserControl class provides an empty control that can be used to create other controls. It is an indirect child of the Control class. 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
The 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 |

