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

An Introduction to Programming a VB.NET Control With Inheritance
How the Better Control is Better

By Dan Mabbutt, About.com

May 29 2009

The previous pages of this article explained how to code two versions of an inherited Visual Basic control. This page tells you why the latest version is better.

Problem number 1 is solved by simply using the properties of the CheckBox that are inherited and calculating the arguments passed to the CenterSquare object (an instance of the GDI+ Rectangle object) that is created in the class.

Problem 2 introduces a whole new topic: Creating new properties in the class. This is something the old class didn't do at all.

The two new properties introduced are

  • FillColor

and

  • FillImage

To get a flavor of how this works in VB.NET, try this simple experiment. Add a class to a standard project and then enter the code:

Public Property Whatever

Press Enter --

VB.NET Intellisense fills in the entire Property code block and all you have to do is code the specifics for your project.

Public Property Whatever()
   Get

   End Get
   Set(ByVal Value)

   End Set
End Property

The purpose of these blocks of code is to allow property values to be accessed from other parts of the system. With the addition of Methods, you would be well on the way to creating a complete component. To see a very simple example of a Method, add this code below the Property declarations in the betterCheckBox class:

Public Sub Emphasize()
   Me.Font = New System.Drawing.Font( _
      "Microsoft Sans Serif", 14.0!, _
      System.Drawing.FontStyle.Bold)
   Me.Size = New System.Drawing.Size(200, 60)
End Sub

To use the new method, just code it the same way:

MyCheckBox.Emphasize()

And just like Properties, the Method is automatically added to Microsoft's Intellisense:

(Thanks for the inspiration to Matt Tagliaferri in his excellent chapter from the VB.NET book, Visual Basic .NET Complete. Thanks also to About Visual Basic reader, "Norseman" for suggesting improvements to Matt's code.)

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.