1. Technology

Inheriting Controls in VB.NET

Improving the code from Microsoft (or a third party control supplier)

By

Young woman working on computer
Innocenti/Cultura/Getty Images
Updated July 02, 2014

One big improvement in Visual Basic .NET is the great new controls in the Toolbox. Controls like WebBrowser (new in VB 2005!) make your programming life easier and more automatic. Even standard forms like Button and TextBox are vastly improved from their VB 6 versions.

But what if a control just isn't quite right? What if you would like a reusable control that has just a few differences? This article explains how to make that happen for you. (And describes how to get around what seems to be a serious bug in Microsoft's Visual Studio .NET 2005. More on this later.)

The techniques described in this article are not possible with VB Express, unfortunately, because Microsoft doesn't provide a template for Windows Control Libraries in VBE.

First, credit where credit is due. In writing this article, I used Mastering Microsoft Visual Basic 2005 (ISBN: 0782143490) by Evangelos Petroutsos extensively. I consulted two other "thick" VB 2005 books for the same information and only this one delivered the goods.

Why would you want to create your own control? There are probably more reasons than there are programmers. But let's look at some typical examples.

  • Suppose there is a common form that everybody uses. (When I was in the US Army, I learned that every company clerk uses the exact same form, called the "Morning Report," to tell the Army who is there and who isn't.) You could create a component that has all the textboxes and labels for this form - and validation for them too - and distribute it to any other programmers who need it.
  • You might want to provide automatic variations of the control based on some information in the application like different defaults or options depending on a user indentification.
  • Or you might want to take care of some standard nagging problem in a standard way. Later on in this page, I show you how to create a control for a standard Windows form that loads the About Visual Basic page as soon as the form is initialized using the new WebBrowser control without loading all of the pop-up ads too!

There are three basic ways that you can create a custom Windows Forms control in VB.NET.

  1. Create a Control Library DLL and inherit from an existing control
  2. Build a control by adding controls to a UserControl
  3. Code a complete control yourself by adding code to the basic Framework Control class

This article explores the first method. (The CalcPad control, explained in the article User Control Components in VB.NET explains the second method.)

Let's create a very basic control by inheriting what might be the most common control, Button, to get the technique down first and also, to see what to do about a problem I ran into.

The rapid fire description of the technique goes like this:

  1. Create a Windows Control Library project.
  2. Edit the hidden "designer" vb file and change the "inherits" clause to specify the control you want to modify instead.
  3. Fix the bug! (Again, more later.)
  4. Add logic to the code for the control and compile the component.

Your control is ready to use!

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Inheriting Controls in VB.NET

©2014 About.com. All rights reserved.