1. Computing

Inheriting Controls in VB.NET

The Basic Method

From , former About.com Guide

Let's do that slowly with a Button control.

Create a Windows Control Library project.

In the illustration below, I named mine AboutVB_Button. I renamed the control itself AVB_Button.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Edit the hidden "designer" vb file and change the "inherits" clause to specify the control that you want to modify instead.

The "designer" vb file is one of the hidden files so it's necessary to display the file so it can be edited.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Fix the bug.

The "inherits" clause is right after the Partial Class code. Change UserControl to Button. (Note that Intellisense can give you a selection of objects to choose from when you enter a period right after System.Windows.Forms.)

Here's where the bug shows up. As the illustration below shows, Visual Studio has an immediate problem with this change. All the references I have seen say it should work just great ... but it doesn't. Here, I would like to appeal to all readers to help me out. If I have misinterpreted something or if you can shed some additional light on this, please let me know. But at this point in time, I'm of the opinion that Microsoft's Visual Studio 2005 has a bug in it!

The illustration below shows what happens:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

(For those who just need more, I believe the problem is created because Visual Studio marks the AutoScaleMode property as Browsable(false) and DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden). This means that the property is not persisted for controls that derive from ContainerControl.)

Fortunately, AutoScaleMode isn't a required property and the default - None - works just fine for most things. So, to work around this problem just skip down to the offending line and comment it out.

   ' Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

The designer window still won't display correctly, but you can Build the Control Library DLL without errors now.

The last step is to add the code that makes your control unique. To demonstrate a couple of possibilities, I've added some code to the New event where the control is instantiated to make the ForeColor property Crimson. I've also modified the Click event to change the ForeColor to White and the BackColor to CadetBlue when the mouse is clicked.

Public Class AVB_Button
   Public Sub New()
      ' This call is required by the Windows Form Designer.
      InitializeComponent()
      ' Add any initialization after the InitializeComponent() call.
      Me.ForeColor = Color.Crimson
   End Sub

   Private Sub AVB_Button_Click( _
      ByVal sender As Object, _
      ByVal e As System.EventArgs) _
      Handles Me.Click
      Me.ForeColor = Color.White
      Me.BackColor = Color.CadetBlue
   End Sub
End Class

Your control is ready to use as after you "Build" the solution!

The easiest way to test out the control is to add a regular Windows project to this solution. (Make sure that you make this the StartUp project. Right-click the Windows project and select Set as Startup Project.) This allows the DLL for the control to be recognized by Visual Studio and automatically placed in the Toolbox. Here's a compact view of Visual Studio with the new control in Toolbox, on a form, and the test project to try it out in Solution Explorer. Since the control is in a DLL, you can distribute it with the Add/Remove Items option in Visual Studio 2003 or the Customize Toolbox option in Visual Studio 2002.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

©2013 About.com. All rights reserved.