oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

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 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:

The background color of the control, represented by a System.Drawing.Color object. You can programmatically assign a System.Drawing.Color object 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 True.

The position of the top-left corner of the control in its container, represented by a System.Drawing.Point object.

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 Button1 to 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 System.Drawing.Size object.

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.Graphics object of the control, on which you can draw using the various methods of the System.Drawing.Graphics class. For instance, the following code obtains the Graphics object 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 Visible property to False, so that it is not shown.

Returns the next control in the tab order.

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, and TextChanged. For example, calling the OnClick method of the control will trigger its Click event.

Sets the control's Visible property to True, so that the control is shown.

The UserControl Class

The UserControl 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.


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

Next Pagearrow