Building complete custom components can be a very advanced project. But you can build a VB.NET class that has many of the advantages of a toolbox component with much less effort. This article shows you how, but in addition, it's a great "getting started" project that will teach you a lot about how classes and inheritance in VB.NET.
To get a flavor of what you need to do to create a complete custom component, try this experiment:
-> Open a new Windows Application project in VB.NET.
-> Add a CheckBox from the Toolbox to the form.
-> Click the "Show All Files" button at the top of Solution Explorer.
This will display the files that Visual Studio creates for your project (so you don't have to). As a historical footnote, The VB6 compiler did a lot of the same things, but you never could access the code because it was buried in compiled "p-code". You could develop custom controls in VB6 too, but it was a lot more difficult and required a special utility that Microsoft supplied just for that purpose.
In the Form Designer.vb file, you will find that the code below has been added automatically in the right locations to support the CheckBox component. (If you have a different version of Visual Studio, your code might be slightly different.) This is the code that Visual Studio writes for you.
'Required by the Windows Form Designer Private components _ As System.ComponentModel.IContainer 'NOTE: The following procedure is required 'by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.CheckBox1 = New System.Windows.Forms.CheckBox() Me.SuspendLayout() ' 'CheckBox1 ' Me.CheckBox1.AutoSize = True Me.CheckBox1.Location = New System.Drawing.Point(29, 28) Me.CheckBox1.Name = "CheckBox1" . . . and so forth ...
This is the code that you have to add to your program to create a custom control. Keep in mind that all the methods and properties of the actual CheckBox control are in a class supplied by the .NET Framework: System.Windows.Forms.CheckBox. This isn't part of your project because it's installed in Windows for all .NET programs. But there's a lot of it.
Another point to be aware of is that if you're using WPF (Windows Presentation Foundation), the .NET CheckBox class comes from a completely different library named System.Windows.Controls. This article only works for a Windows Forms application, but the principals of inheritance here work for any VB.NET project.
Suppose your project needs a control that is very much like one of the standard controls. For example, a checkbox that changed color, or displayed a tiny "happy face" instead of displaying the little "check" graphic. We're going to build a class that does this and show you how to add it to your project. While this might be useful by itself, the real goal is to deomontrate VB.NET's inheritance.
Let's Start Coding!
To get started, change the name of the CheckBox that you just added to oldCheckBox. (You might want to stop displaying "Show All Files" again to simplify Solution Explorer.) Now add a new class to your project. There are several ways to do this including right-clicking the project in Solution Explorer and selecting "Add" then "Class" or selecting "Add Class" under under the Project menu item. Change the file name of the new class to newCheckBox to keep things straight. Finally, open the code window for the class and add this code:
Public Class newCheckBox Inherits CheckBox Private CenterSquareColor As Color = Color.Red Protected Overrides Sub OnPaint( ByVal pEvent _ As PaintEventArgs) Dim CenterSquare _ As New Rectangle(3, 4, 10, 12) MyBase.OnPaint(pEvent) If Me.Checked Then pEvent.Graphics.FillRectangle( New SolidBrush( CenterSquareColor ), CenterSquare) End If End Sub End Class
(In this article and in others on the site, a lot of line continuations are used to keep lines short so they will fit into the space available on the web page.)
The first thing to notice about your new class code is the Inherits keyword. That means that all the properties and methods of a VB.NET Framework CheckBox are automatically part of this one. To appreciate how much work this saves, you have to have tried programming something like a CheckBox component from scratch.
There are two key things to notice in the code above:
The first is the code uses Override to replace the standard .NET behavior that would take place for an OnPaint event. An OnPaint event is triggered whenever Windows notices that part of your display has to be reconstructed. An example would be when another window uncovers part of your display. Windows updates the display automatically, but then calls the OnPaint event in your code. (The OnPaint event is also called when the form is initially created.) So if we Override OnPaint, we can change the way things look on the screen.
The second is the way Visual Basic creates the CheckBox. Whenever the parent is "Checked" (that is, Me.Checked is True) then the new code we provide in our NewCheckBox class will recolor the center of the CheckBox instead of drawing a checkmark.
The rest is what is called GDI+ code. This code selects a rectangle the exact same size as the center of a Check Box and colors it in with GDI+ method calls. (GDI+ is covered in a different tutorial: GDI+ Graphics in Visual Basic .NET. The "magic numbers" to position the red rectangle, "Rectangle(3, 4, 10, 12)", were determined experimentally. I just changed it until it looked right.
There is one very important step that you want to make sure you don't leave out of Override procedures:
Override means that your code will provide all of the code for the event. But this is seldom what you want. So VB provides a way to run the normal .NET code that would have been executed for an event. This is the statement that does that. It passes the very same parameter - pEvent - to the event code that would have been executed if it hadn't been overridden - MyBase.OnPaint.
On the next page, we put the new control to use!