Walkthrough: Inheriting from a Windows Forms Control with Visual Basic .NET
Visual Basic .NET provides you with the ability to create powerful custom controls through inheritance. Through inheritance you are able to create controls that retain all of the inherent functionality of standard Windows Forms controls but also incorporate custom functionality. In this walkthrough, you will create a simple inherited control called ValueButton. This button will inherit functionality from the standard Windows Forms button, and will expose a custom property called ButtonValue.
Creating the Project
When you create a new project, you specify its name in order to set the root namespace, assembly name, and project name, and to ensure that the default component will be in the correct namespace.
To create the ValueButtonLib control library and the ValueButton control
- On the File menu, point to New and then click Project to open the New Project dialog box.
- Select the Windows Control Library project template from the list of Visual Basic projects, and type ValueButtonLib in the Name box.
The project name, ValueButtonLib, is also assigned to the root namespace by default. The root namespace is used to qualify the names of components in the assembly. For example, if two assemblies provide components named ValueButton, you can specify your ValueButton component using
ValueButtonLib.ValueButton. For more information, see Components and Assemblies.
- In Solution Explorer, right-click UserControl1.vb and select View Code from the shortcut menu.
- Locate the Class statement,
Public Class UserControl1, and change UserControl1 to ValueButton to change the name of the component.
- Locate the statement
Inherits System.Windows.Forms.UserControl, and change System.Windows.Forms.UserControl to System.Windows.Forms.Button. This allows your inherited control to inherit all the functionality of the Button control.
- In Solution Explorer, click UserControl1.vb and in the Properties window, change the FileName property to ValueButton.vb.
- From the File menu, choose Save All to save the project.
Note that the User Control Designer is no longer available. Because the Button control does its own painting, you are unable to modify its appearance in the designer. Its visual representation will be exactly the same as that of the class it inherits from (that is, Button) unless modified in the code.
Adding a Property to Your Inherited Control
One possible use of inherited Windows Forms controls is the creation of controls that are identical in look and feel of standard Windows Forms controls, but expose custom properties. In this section, you will add a property called ButtonValue to your control.
To add the Value property
- In Solution Explorer, right-click ValueButton.vb, and then click View Code from the shortcut menu.
- Locate the
Inherits System.Windows.Forms.Buttonstatement. Immediately beneath this statement, type the following code:
' Creates the private variable that will store the value of your ' property. Private varValue as integer ' Declares the property. Property ButtonValue() as Integer ' Sets the method for retrieving the value of your property. Get Return varValue End Get ' Sets the method for setting the value of your property. Set(ByVal Value as Integer) varValue = Value End Set End Property
This code sets the methods by which the ButtonValue property is stored and retrieved. The Get statement sets the value returned to the value that is stored in the private variable varValue, and the Set statement sets the value of the private variable by use of the Value keyword.
- From the File menu, choose Save All to save the project.
Testing Your Control
Controls are not stand-alone projects; they must be hosted in a container. In order to test your control, you must provide a test project for it to run in. You must also make your control accessible to the test project by building (compiling) it. In this section, you will build your control and test it in a Windows Form.
To build your control
- On the Build menu, click Build.
The build should be successful with no compiler errors or warnings.
To create a test project
- On the File menu, point to Add Project and then click New Project to open the Add New Project dialog box.
- Select the Visual Basic projects node, and click Windows Application.
- In the Name box, type Test.
- In Solution Explorer, right-click the References node for your test project, then select Add Reference from the shortcut menu to display the Add Reference dialog box.
- Click the Projects tab.
- Double-click your project, and note that it now appears in the Selected Components pane of the dialog box.
After you have added your reference, you should add your new control to the Toolbox.
To add your control to the Toolbox
- Right-click the Toolbox and choose Add/Remove Items from the shortcut menu.
The Customize Toolbox dialog box opens.
- Choose the .NET Framework Components tab and click Browse.
- Browse to the ValueButtonLib\bin folder and select ValueButton.dll.
ValueButton appears in the list of components in the Customize Toolbox dialog box.
- In the Customize Toolbox dialog box, check the box next to ValueButton and close the window.
The ValueButton is added to the tab of the Toolbox that was selected.
To add your control to the form
- In Solution Explorer, right-click Form1.vb and choose View Designer from the shortcut menu.
- In the Toolbox, scroll down until you come to the icon labeled ValueButton. Double-click this icon.
A ValueButton appears on the form.
- Right-click the ValueButton and select Properties from the shortcut menu.
- In the Properties window, examine the properties of this control. Note that they are identical to the properties exposed by a standard button, except that there is an additional property, ButtonValue.
- Set the ButtonValue property to 5.
- In the Windows Forms tab of the Toolbox, double-click Label to add a Label control to your form.
- Relocate the label to the center of the form.
- Double-click ValueButton1.
The Code Editor opens to the ValueButton1_Click event.
- Type the following line of code.
Label1.Text = CStr(ValueButton1.ButtonValue)
- In Solution Explorer, right-click Test, and choose Set as Startup Project from the shortcut menu.
- From the Debug menu, select Start.
- Click Valuebutton1.
The numeral '5' is displayed in Label1, demonstrating that the ButtonValue property of your inherited control has been passed to Label1 via the ValueButton1_Click method. Thus your ValueButton control inherits all the functionality of the standard Windows Forms button, but exposes an additional, custom property.