Updated for Framework 1.0
Visual Basic 2005 Express
Including Version 3
of the About Visual Basic Calculator
User Control is a cool technology! Just a few months after .NET 1.0 was released, About Visual Basic published an article showing how to create your own user controls in VB.NET. Here's a quote:
VB.NET makes programming user controls so easy that the everybody will soon be taking it for granted. "Isn't that the way it's always been?"
Easy? Yes! Obvious? No.
First, creating user controls has changed since VB.NET 2.0 and Visual Basic 2005 Express came out. Second, there are different procedures for creating these handy objects between those two products. But the good news is that even the free VB.NET, Visual Basic Express 2005, can create user controls without too much trouble.
To update the site, I've completely reworked the original user control article to add information about the changes in control creation VB.NET 2.0 and VBE 2005 and to add new code downloads.
In addition, the example application I've used for user controls, the About Visual Basic Calculator, has been completely reprogrammed too. (I learn better ways to do things as I write these articles.)
But before we get into all that, let's review just exactly what a user control is.
A user control is just like the Visual Basic supplied controls, such as TextBox or Button, but you can make your own control do whatever you like with your own code. (You can also create web user controls -- they're not the same as a "Web custom controls" -- but this article only covers the creation of user controls for Windows.) Think of user controls as 'bundles' of standard controls with custom methods and properties. Whenever you have a group of controls that you're likely to use in more than one place, consider a user control.
In more detail, a user control is a VB.NET class. The class Inherits from the Framework UserControl class. The UserControl class gives your control the base functions it needs so it can be treated like the built-in controls. A user control also has a visual interface, much like a VB.NET form that you design in VB.NET.
To demonstrate a user control, we're going to create our own four function calculator control that you can drag and drop right onto a form in your project. If you have a financial application where it would be handy to have a custom calculator available, you can add your own code to this one and use it just like a Toolbox control in your projects. With your own calculator control, you could add keys that automatically input a company standard such as a required rate of return or add the corporate logo to the calculator.
See the illustration below to see what the About Visual Basic calculator looks like!
Click Here to display the illustration
Click the Back button on your browser to return
The first step in creating a user control is to program a standard Windows application that does what you need. Although there are some extra steps, it's still often easier to program your control first as a standard Windows application than as a user control since you can debug more easily. Once you have your application working, you can copy the code to a user control class and build the user control as a DLL file. These basic steps are the same in all versions since the underlying technology is the same, but the exact procedure is a little different between VB.NET versions. Let's see how to do it in all versions.