You are here:About>Computing & Technology>Visual Basic
About.comVisual Basic
A First Introduction to VB.NET Controls
Create A Custom Colored Check Box!
The New Custom CheckBox

Building complete custom components that react to events, have a complete set of properties and methods, and are in your toolbox when you need them can be a time consuming project. But you can build a VB.NET class that has many of the advantages of a toolbox component with much less effort. In addition, it's a great "getting started" project that will teach you a lot about how classes and inheritance in the new VB.NET works.

When you digest this one, check out the 'next one' ... About Visual Basic readers took this idea and ran with it and created even better CheckBox controls in this followup article.


 Related Resources
• 
More Customizing VB.NET Controls

The sequel to this article!
• 
Need a "getting started" guide for VB.NET?

• 
Upgrading to VB.NET

• 
Get VB.NET Cheap!

 


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

  • Open a new WindowsApplication project in VB.NET.
  • Add a Check Box from the Toolbox to the form. (We're going to use it later anyway.)
  • Open the code window and click on the plus sign to the left of the
       #Region " Windows Form Designer generated code "
    to display it. Notice that all the code below has been added automatically in the right locations to support the Check Box.
  Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
         
      Me.CheckBox1 = New System.Windows.Forms.CheckBox()
      Me.SuspendLayout()
      '
      'CheckBox1
      '
      Me.CheckBox1.Location = New System.Drawing.Point(56, 64)
      Me.CheckBox1.Name = "CheckBox1"
      Me.CheckBox1.Size = New System.Drawing.Size(168, 56)
      Me.CheckBox1.TabIndex = 0
      Me.CheckBox1.Text = "CheckBox1"
         
      Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.CheckBox1})
         
      Me.ResumeLayout(False)

This, and 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 Foundation for your project.

But suppose you just want to add a little customization? Like, for instance, a checkbox that changed color instead of displaying the little "check" graphic when you use it as in the illustration above. We're going to build a class that does just this much and show you how to add it to your project in code. While this might be useful by itself, the real goal is to explain VB.NET's new "inheritance" capability to simply change the behavior of the existing Check Box control.

Next page > Get Started Coding! > Page 1, 2, 3
From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!
Newsletters & RSSEmail to a friendSubmit to Digg
 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.