Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Leverage Custom Controls
Learn the basic types of custom controls, as well as how to extend them. Also, learn how to take fuller advantage of drag-and-drop within your applications.
by Stephen Perry

January 2, 2006

Technology Toolbox: C#

This article is excerpted from Chapter 7, "Windows Forms Controls," of Stephen Perry's book, Core C# and .NET, with permission from Prentice Hall [2005, ISBN: 0131472275]. It has been edited for length and format to fit the magazine. You can read a PDF of the full chapter here.

At some point, you will face a programming task for which a standard WinForms control does not provide the functionality you need. For example, you might want to extend a TextBox control so that its background color changes according to its content; group a frequently used set of radio buttons into a single control; or create a new control that shows a digital clock face with the date underneath. These needs correspond to the three principal types of custom controls: a control that derives from an existing control and extends its functionality; a control that can serve as a container to allow multiple controls to interact (a user control); and a control that derives directly from the Control class. The third type of control is one that is built "from scratch," and it is the developer's responsibility to draw its GUI interface and implement the methods and properties that allow it to be manipulated by code.

I'll walk you through how to implement each of these types of controls, as well as provide some important background information on how to manipulate and extend these same controls. I'll also show you how to take advantage of drag-and-drop controls, for which Visual Studio 2005 includes robust support.

Let's begin with the easiest way to create a custom control: extending an existing control. Specifically, let's derive a TextBox that accepts only digits. The code is quite simple. Create a new NumericTextBox class with TextBox as its base class. The only code required is an event handler to process the KeyPress event and accept only a digit:

class NumericTextBox: TextBox 
{
   public NumericTextBox() 
   {
      this.KeyPress += new 
         KeyPressEventHandler(TextBoxKeyPress);
   }
   protected void TextBoxKeyPress(object sender, 
      KeyPressEventArgs e)
   {
      if (! char.IsDigit(e.KeyChar)) e.Handled = true;
   }
}
ADVERTISEMENT

You can add the extended control to any form once you compile it into a DLL file.

Building a custom user control is also straightforward. Think of a user control as a subform. Like a form, it provides a container surface on which related widgets are placed. When compiled, the entire set of controls is treated as a single user control. Of course, users still can interact directly with any of the member controls. Programmatic and design-time access to control members is available through methods and properties defined on the user control.

The easiest way to design a control is with an IDE such as Visual Studio .NET, which makes it easy to position and size controls. The usual way to create a user control in VS.NET is to open a project as a Windows Control Library type. This brings up a control designer window immediately. You can also access the design window in a Windows application by selecting Project | Add User Control from the top menu bar or by right-clicking on the Solution Explorer and selecting Add | Add User Control. VS.NET can speed up the process of creating a control, but it doesn't generate any proprietary code that you can't duplicate using a text editor.




Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home