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

An Introduction to Programming a VB.NET Control With Inheritance
Using the New Control

By Dan Mabbutt, About.com

May 29 2009

On the previous page, this article showed how to create a custom control using VB.NET and inheritance.

Because our new control is not in our toolbox, we have to create it on the form with code. The best place to do this is in the form Load event procedure. Here's how to code that:

Open the code window for the form load event procedure and add this code:

Private Sub Form1_Load(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles MyBase.Load
   Dim MyCheckBox As New newCheckBox()
   With MyCheckBox
   .Text = "Custom Colored Check Box"
   .Left = oldCheckBox.Left
   .Top = oldCheckBox.Top + OldCheckBox.Height
   .Size = oldCheckBox.Size
   End With
   Controls.Add(MyCheckBox)
End Sub

To place the new checkbox on the form, we've taken advantage of the fact that there is already one one there and just used the size and position of that one. Otherwise we would have to code the position manually. When MyCheckBox has been added to the form, we then add it to the Controls collection.

But there are several problems with this code.

  • It doesn't work unless the default position of the Checkbox (RightToLeft = No) is used. The hardcoded "Dim CenterSquare As New Rectangle(2, 3, 9, 9)" causes this problem.
  • The color Red is hardcoded and changing the color requires changing the program.

Here's a new, improved CheckBox class. I won't explain this one completely, but it shows you how to take one of the next steps toward VB.NET object oriented coding.

Public Class betterCheckBox
   Inherits CheckBox
   '
   'Syntax:
   '
   ' object.FillImage = New Bitmap(Filename:="filename")
   ' object.FillColor = Color.colorname
   ' The Bitmap object file can be of several different
   ' formats and any size.
   ' It's recommended that you have an image close to 9x9
   ' pixels because GDI+ will resize the image to that size
   ' automatically and this can produce horrendous results.
   ' If the FillImage property is not provided, the FillColor
   ' will be used to fill the box.
   ' If the FillColor property is also missing,
   ' a standard checkmark is displayed.
   '
   Private CenterSquareColor As Color
   Private CenterSquareImage As Bitmap

   Protected Overrides Sub OnPaint _
      (ByVal pEvent As System.Windows.Forms.PaintEventArgs)

      ' Allow for any size checkbox to be properly filled
      ' The IIF Statement determines which side of the
      ' CheckBox object the box is actually on.
      ' The (Me.Height / 2) - 4 finds the top of the area
      ' to be filled in
      ' The 9, 9 is the height and width of the
      ' interior of the CheckBox box.
      Dim CenterSquare As New Rectangle( _
         IIf(Me.RightToLeft = RightToLeft.No, _
         2, Me.Width - 12), (Me.Height / 2) - 5, 9, 9)
      MyBase.OnPaint(pEvent)
      If Me.Checked Then
         ' Fill in the box with the predetermined
         ' Color() (using a solid brush)
         If CenterSquareImage Is Nothing Then
            pEvent.Graphics.FillRectangle( _
               New SolidBrush( _
               CenterSquareColor), CenterSquare)
         Else
            ' Fill in the box with the an the
            ' image that was passed
            pEvent.Graphics.DrawImage( _
               CenterSquareImage, CenterSquare)
         End If
      End If
   End Sub

   Public Property FillColor() As Color
      Get
         FillColor = CenterSquareColor
      End Get
      Set(ByVal Value As Color)
         CenterSquareColor = Value
      End Set
   End Property

   Public Property FillImage() As Bitmap
      Get
         FillImage = CenterSquareImage
      End Get
      Set(ByVal Value As Bitmap)
         CenterSquareImage = Value
      End Set
   End Property
End Class

On the next page, some of the features of the new, improved code are explained.

Explore Visual Basic
By Category
About.com Special Features

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

Easy ways to connect two computers for networking purposes. 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.