1. Home
  2. Computing & Technology
  3. Visual Basic

An Introduction to Programming a VB.NET Control With Inheritance
Create A Custom CheckBox Control!

By , About.com Guide

May 29 2009

Building complete custom components can be a very advanced project. But you can build a VB.NET class that has many of the advantages of a toolbox component with much less effort. This article shows you how, but in addition, it's a great "getting started" project that will teach you a lot about how classes and inheritance in VB.NET.

To get a flavor of what you need to do to create a complete custom component, try this experiment:

  • Open a new Windows Application project in VB.NET.
  • Add a CheckBox from the Toolbox to the form.
  • Click the "Show All Files" button at the top of Solution Explorer.

This will display the files that Visual Studio creates for your project (so you don't have to). As a historical footnote, The VB6 compiler did a lot of the same things, but you never could access the code because it was buried in compiled "p-code".

In the Form1.Designer.vb file, you will find that the code below has been added automatically in the right locations to support the CheckBox component.

Private Sub InitializeComponent()
   Me.CheckBox1 = New System.Windows.Forms.CheckBox
   Me.SuspendLayout()
   '
   'CheckBox1
   '
   Me.CheckBox1.AutoSize = True
   Me.CheckBox1.Location = New System.Drawing.Point(25, 28)
   Me.CheckBox1.Name = "CheckBox1"
   Me.CheckBox1.Size = New System.Drawing.Size(81, 17)
   Me.CheckBox1.TabIndex = 0
   Me.CheckBox1.Text = "CheckBox1"
   Me.CheckBox1.UseVisualStyleBackColor = True
   ... etc.
End Sub
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox

This, and a lot more, is what you have to do to use a complete custom control. Keep in mind that all the methods and properties of the Check Box are in a class supplied by the .NET Framework for your project: the System.Windows.Forms.CheckBox class and you can't see that code. There's a lot of it.

But suppose your project needs a control that is very much like one of the standard controls. For example, a checkbox that changed color instead of displaying the little "check" graphic when you use it. We're going to build a class that does just this much and show you how to add it to your project. While this might be useful by itself, the real goal is to deomontrate VB.NET's inheritance.

Let's Start Coding!

To get started, change the name of the CheckBox that you just added to oldCheckBox. (You might want to click "Show All Files" again to simplify the Solution Explorer display.) Now add a new class to your project. There are several ways to do this including right-clicking the project in Solution Explorer and selecting "Add" then "Class" or selecting "Add Class" under under the Project menu item. Change the file name of the new class to newCheckBox to keep things straight. Finally, open the code window for the class and add this code:

Public Class newCheckBox
   Inherits CheckBox
   Private CenterSquareColor As Color = _
      Color.Red
   Protected Overrides Sub OnPaint( _
      ByVal pEvent As _
         System.Windows.Forms.PaintEventArgs)
      Dim CenterSquare As New _
         Rectangle(2, 3, 9, 9)
      MyBase.OnPaint(pEvent)
      If Me.Checked Then
         pEvent.Graphics.FillRectangle( _
         New SolidBrush( _
            CenterSquareColor), _
            CenterSquare)
      End If
   End Sub
End Class

(In this article and in others on the site, a lot of line continuations are used to keep lines short so they will fit into the space available on the web page.)

The first thing to notice about your new class code is the "Inherits" keyword. That means that all the properties and methods of a standard CheckBox are automatically part of your new one. You have to have tried programming something like a CheckBox component from scratch to really understand how much of a benefit this is.

So ... how do we change the behavior to make the center square colored? There are two keys that we use to accomplish this.

The first is to "Override" the behavior of the OnPaint event. Windows notices when any part of your display has to be reconstructed. An example would be when another window uncovers part of your display. The OnPaint event rebuilds the display automatically. The OnPaint event is also called when the form is initially created. So if we override OnPaint, we can change the way things look on the screen.

The second is to change the way Visual Basic creates the CheckBox. Whenever the parent is "Checked" (Me.Checked) then the new code we provide in our NewCheckBox class will recolor the center of the CheckBox instead of drawing a checkmark.

The rest is standard GDI+ code to select a rectangle the exact same size as the center of a Check Box and color it in with GDI+ method calls. (GDI+ is covered in a different tutorial: GDI+ Graphics in Visual Basic .NET. The "magic numbers" to position the red rectangle, "Rectangle(2, 3, 9, 9)", were determined experimentally. I just changed it until it looked right.

There is one very important step that you want to make sure you don't leave out of Override procedures:

MyBase.OnPaint(pEvent)

Override assumes that your code will provide all the code for the event. But this is seldom what you want. So VB provides a way to run the normal code that would have been executed. This is the statement that does that. It passes the very same parameter (pEvent) to the event code that would have been executed if it hadn't been overridden (MyBase.OnPaint).

On the next page, we put the new control to use!

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Programming a Class to Create a VB.NET Control Object

©2009 About.com, a part of The New York Times Company.

All rights reserved.