VB.NET Control With Inheritance

VB.NET Control ,Building complete custom additives can be a very superior project. But you may construct a VB.NET magnificence that has the various advantages of a toolbox factor with a good deal less attempt. Here’s how!

To get a flavor of what you want to do to create a whole custom component, do this test:

  1. Open a new Windows Application assignment in VB.NET.
  2. Add a CheckBox from the Toolbox to the form.
  3. Click the “Show All Files” button on the top of Solution Explorer.

This will show the files that Visual Studio creates for your task (so you do not should). As a historical footnote, The VB6 compiler did numerous the same matters, however you in no way should get admission to the code because it was buried in compiled “p-code”. You ought to expand custom controls in VB6 too, however it turned into loads extra difficult and required a unique software that Microsoft provided just for that motive.

In the Form Designer.Vb record, you will discover that the code underneath has been added routinely inside the proper locations to support the CheckBox element. (If you’ve got a special version of Visual Studio, your code is probably slightly special.) 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 software to create a custom manage. Keep in mind that every one the strategies and houses of the actual CheckBox manipulate are in a class furnished by means of the .NET Framework: System.Windows.Forms.CheckBox. This isn’t always a part of your project because it’s installed in Windows for all .NET programs. But there may be a variety of it.

Another point to be aware of is that if you’re the use of WPF (Windows Presentation Foundation), the .NET CheckBox magnificence comes from a completely specific library named System.Windows.Controls. This article best works for a Windows Forms utility, however the principals of inheritance here work for any VB.NET mission.

Suppose your challenge wishes a control this is very just like one in all the standard controls. For instance, a checkbox that changed coloration, or displayed a tiny “happy face” in preference to showing the little “check” photo. We’re going to build a class that does this and show you the way to upload it to your undertaking. While this is probably useful via itself, the real aim is to demonstrate VB.NET’s inheritance.

VB.NET Control Let’s Start Coding

VB.NET Control To get started out, trade the name of the CheckBox which you just brought to oldCheckBox. (You would possibly want to stop showing “Show All Files” once more to simplify Solution Explorer.) Now upload a new elegance to your challenge. There are several approaches to do this which include right-clicking the task in Solution Explorer and selecting “Add” then “Class” or choosing “Add Class” underneath underneath the Project menu item. Change the file name of the brand new magnificence to newCheckBox to hold matters immediately. Finally, open the code window for the elegance and upload 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 SubEnd Class

(In this newsletter and in others at the website online, plenty of line continuations are used to hold lines brief so they will in shape into the space available on the net page.)

The first thing to be aware about your new class code is the Inherits key-word. That means that every one the houses and techniques of a VB.NET Framework CheckBox are mechanically part of this one. To recognize how a whole lot paintings this saves, you need to have tried programming something like a CheckBox element from scratch.

There are two key matters to note inside the code above:

VB.NET Control The first is the code makes use of Override to replace the usual .NET behavior that might take vicinity for an OnPaint occasion. An OnPaint occasion is brought about on every occasion Windows notices that a part of your show must be reconstructed. An instance would be whilst some other window uncovers part of your show. Windows updates the display routinely, but then calls the OnPaint occasion in your code. (The OnPaint event is likewise known as whilst the shape is initially created.) So if we Override OnPaint, we will exchange the way things look at the display.

The 2nd is the way Visual Basic creates the CheckBox. Whenever the determine is “Checked” (this is, Me.Checked is True) then the brand new code we provide in our NewCheckBox class will recolor the middle of the CheckBox in preference to drawing a checkmark.

The rest is what is referred to as GDI+ code. This code selects a rectangle the exact same length as the center of a Check Box and colorations it in with GDI+ technique calls. The “magic numbers” to put the pink rectangle, “Rectangle(three, four, 10, 12)”, have been decided experimentally. I just modified it until it seemed right.

There is one very essential step that you want to make certain you do not miss of Override tactics:

MyBase.OnPaint(pEvent)

VB.NET Control Override method that your code will provide all of the code for the occasion. But this is seldom what you need. So VB provides a manner to run the regular .NET code that would have been carried out for an event. This is the announcement that does that. It passes the very equal parameter—pEvent—to the event code that would had been carried out if it hadn’t been overridden, MyBase.OnPaint.

VB.NET Control Using the New Control

VB.NET Control Because our new control isn’t in our toolbox, it must be created in the shape with code. The quality location to do that is within the form Load occasion system.

Open the code window for the form load event procedure and upload this code:

Private Sub frmCustCtrlEx_Load( ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles MyBase.Load Dim customCheckBox As New newCheckBox() With customCheckBox .Text = "Custom CheckBox" .Left = oldCheckBox.Left .Top = oldCheckBox.Top + oldCheckBox.Height .Size = New Size( oldCheckBox.Size.Width + 50, oldCheckBox.Size.Height) End With Controls.Add(customCheckBox)End Sub

VB.NET Control To area the new checkbox on the shape, we have taken advantage of the truth that there may be already one there and simply used the size and position of that one (adjusted so the Text assets will healthy). Otherwise we would must code the position manually. When MyCheckBox has been added to the form, we then upload it to the Controls series.

But this code isn’t very bendy. For example, the color Red is hardcoded and changing the coloration calls for converting this system. You can also need a photo rather than a test mark.

Here’s a brand new, advanced CheckBox class. This code shows you the way to take some of the subsequent steps toward VB.NET item oriented programming.

Public Class betterCheckBox Inherits CheckBox Private CenterSquareColor As Color = Color.Blue Private CenterSquareImage As Bitmap Private CenterSquare As New Rectangle( 3, 4, 10, 12) Protected Overrides Sub OnPaint _ (ByVal pEvent As _ System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(pEvent) If Me.Checked Then If CenterSquareImage Is Nothing Then pEvent.Graphics.FillRectangle( New SolidBrush( CenterSquareColor), CenterSquare) Else pEvent.Graphics.DrawImage( CenterSquareImage, CenterSquare) End If End If End Sub Public Property FillColor() As Color Get FillColor = CenterSquareColor End Get Set(ByVal Value As Color) CenterSquareColor = Value End Set End Property Public Property FillImage() As Bitmap Get FillImage = CenterSquareImage End Get Set(ByVal Value As Bitmap) CenterSquareImage = Value End Set End PropertyEnd Class

VB.NET Control Why The BetterCheckBox Version Is Better

One of the main improvements is the addition of two Properties. This is some thing the antique elegance did not do at all.

The new houses introduced are

 

FillColor

and

FillImage

To get a flavor of the way this works in VB.NET, do this simple experiment. Add a class to a standard challenge and then input the code:

Public Property Whatever Get

When you press Enter after typing “Get”, VB.NET Intellisense fills in the entire Property code block and all you need to do is code the specifics for your project. (The Get and Set blocks aren’t always required beginning with VB.NET 2010, so you have to as a minimum tell Intellisense this a whole lot to begin it.)

Public Property Whatever Get End Get Set(ByVal value) End SetEnd Property

VB.NET Control These blocks have been finished inside the code above. The motive of those blocks of code is to permit assets values to be accessed from different elements of the machine.

VB.NET Control With the addition of Methods,VB.NET Control you will be properly at the manner to growing a entire component. To see a completely easy instance of a Method, upload this code beneath the Property declarations in the betterCheckBox magnificence:

Public Sub Emphasize() Me.Font = New System.Drawing.Font( _ "Microsoft Sans Serif", 12.0!, _ System.Drawing.FontStyle.Bold) Me.Size = New System.Drawing.Size(200, 35) CenterSquare.Offset( CenterSquare.Left - 3, CenterSquare.Top + 3)End Sub

VB.NET Control In addition to adjusting the Font displayed in a CheckBox, this method also adjusts the size of the box and the area of the checked rectangle to account for the brand new length. To use the brand new method, simply code it the identical way you will any approach:

MyBetterEmphasizedBox.Emphasize()

And similar to Properties, Visual Studio routinely provides the new method to Microsoft’s Intellisense!

VB.NET Control The essential goal here is to certainly reveal how a method is coded. You can be conscious that a standard CheckBox manage also permits the Font to be changed, so this approach doesn’t virtually add plenty characteristic.

The next article on this series, Programming a Custom VB.NET Control – Beyond the Basics!, suggests a technique that does, and also explains a way to override a way in a custom manipulate.