Building complete custom components can be a very advanced project. This article goes beyond the basics, but it still intended for inexperienced VB.NET programmers. If you're new to the concept and you need to start at an easier place, read this article first: An Introduction to Programming a VB.NET Control With Inheritance.
In the previous article (linked above), the principle of Inheritance was used to code a "BetterCheckBox" control that had methods and properties beyond the one available in the ToolBox. The basic idea is to take an existing control, such as a CheckBox, and add new behavior as well as new methods and properties. You can then use the new control in your program with these new features.
Intellisense even recognizes them and helps you use them. Inheritance allows a new class (the new control is a class) to automatically have all of the properties and methods of another class. The new class is called a child class or a subclass. The class that is inherited is called a base class or parent class.
You might want to remember this rule when thinking about inheritance in .NET. A child class "is a" version of the base class. A dog "is a" animal and a beagle "is a" dog. But a sunflower can't be a child of the animal class or the dog class.
In the previous article, the standard CheckBox was changed to allow both a colored rectangle and a "happy face" icon instead of a check mark. The article also demonstrated how a method could be added to the custom CheckBox but the method really didn't do much. In this article, we start with the same BetterCheckBox custom control but we add a more functional method. Actually, we add it, and then we override it.
To set up the situation, suppose you have a form with a lot of CheckBox controls, but the user wants the ability to "move them out of the way" so they're not confusing for some other operation and then move them back to where they were when the operation is done.
A method to do this is really fairly simple. Add this sub to the BetterCheckBox class code from the previous article.
Public Sub Scoot()
Static ScootedFrom As Point
If Me.Location = New Point(0, 0) Then
Me.Location = ScootedFrom
Else
ScootedFrom = Me.Location
Me.Location = New Point(0, 0)
End If
End Sub
Then create several form controls using this revised BetterCheckBox control. When the Scoot method is called for a BetterCheckBox control now ...
customCheckBox1.Scoot()
customCheckBox2.Scoot()
... the control is simply moved to the top left corner of the form. In this case, two are stacked together. To "un-scoot", simply click the button again.
--------
Click Here to display the illustration
--------
"Job Done!" - I congratulate myself and distribute the application. But when you get it, you say, "That's kinda lame! The title of one of the controls is what shows on the stack. I think that the program ought to be able to create a sort of "stack cover" for the controls to show what is there.
VB.NET has you covered. All you have to do is inherit the inherited control again and then override the Scoot method. The process is roughly the same as before. Create another class and inherit the BetterCheckBox control to override the Scoot method. Note that the ScootCover property is actually a Label control. This allows you to override the properties of that Label in the BetterScoot control.
Public Class BetterScoot
Inherits BetterCheckBox
Property ScootCover As New Label
Public Overrides Sub Scoot()
MyBase.Scoot()
ScootCover.Width = MyBase.Width
If ScootCover.Text = "" Then
ScootCover.Text = " ScootCover "
End If
If MyBase.Location = New Point(0, 0) Then
ScootCover.Visible = True
Else
ScootCover.Size = MyBase.Size
ScootCover.Location = New Point(0, 0)
ScootCover.BringToFront()
ScootCover.Visible = False
End If
Controls.Add(ScootCover)
End Sub
End Class
The controls are created in a form Load event using the BetterScoot control instead this way:
Private customCheckBox1 As New BetterScoot
Private customCheckBox2 As New BetterScoot
Private Sub Form1_Load(
ByVal sender As Object,
ByVal e As System.EventArgs
) Handles Me.Load
With customCheckBox1
.Text = " Custom CheckBox 1 "
' Width is in pixels,
' Length is in characters
.Width = .Text.Length * 10
.Left = 50
.Top = 50
.ScootCover.Text =
"This Stack of Controls"
End With
... etc.
If you've been trying out the code as you read the article, you will have discovered that this code doesn't quite work yet. Visual Studio complains that Sub Scoot has not been declared overridable. This is an important design issue that should be considered whenever code might be used by other people. If there is a chance that someone else might have a good reason to change the way your code works, you might want to declare your subroutines as overridable so you don't have to recompile your code. In this case, it looks like this:
Public Overridable Sub Scoot()
The end result now looks a bit more polished.
--------
Click Here to display the illustration
--------


