1. Home
  2. Computing & Technology
  3. Visual Basic

Create Your Own Custom Form Object

Inherit the Form

By Dan Mabbutt, About.com

Aug 11 2007

You must Build the Form first to create either an EXE or a DLL before you can inherit it as an object. In VB.NET, all classes are inheritable unless you mark them otherwise. Since your form is a class, you can use inheritance to define a new form based on the one we just created.

A DLL is necessary if you plan to use it outside the current project. Since we do plan to do that, right-click the project and select Properties. Under the Applications tab, change the Application Type from Windows Application to Class Library. Now when you Build the solution, a DLL will be created in the Release folder under the Bin folder in your solution.

To demonstrate how the form is used, let's create an AVBMainSystem form and inherit a copy of the dialog that we just created. Here's a form I dreamed up that will work to illustrate the process:

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

When your main form is coded, right-click the project in Solution Explorer and select Add and then New Item... from the popup context menu. Select the Inherited Form template, give your new form a name, and click Add.

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

Since the form you want to inherit from isn't in your current project, you'll get an error in the next window saying there is nothing to inherit from. No worries. Just click the Browse button. Navigate to the Release folder where the DLL for your base form is and select it. Visual Studio adds the inherited form into your project automagically! The result is shown below:

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

Using this technique, the components that are inherited, such as ConfirmBtn and CancelBtn, are locked. They work just as they did in the base form. I displayed a simple MsgBox in the base form as shown in the illustration below:

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

But this isn't very useful in a lot of situations. In this case, for example, when the Confirm button is clicked, some code like changing a status flag for the description should run. It would be handy if the modal dialog form closed as well. And the MsgBox stub code in the base form should not run!

One way to accomplish this is to change the Click event code in the base form from Private to Public Overridable like this:

   Public Overridable Sub ConfirmButton_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles ConfirmBtn.Click
      MsgBox("Confirm Code Executed!")
   End Sub

When this code is compiled into a DLL, it can be overriden with whatever code you choose in your main system like this:

Public Class ProjDescConfirm
   Public Overrides Sub ConfirmButton_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs)
      MsgBox("DIFFERENT Confirm Code Executed!")
      Me.Close()
   End Sub
End Class

The finished product looks like this. Notice that the MsgBox is now displayed by the inherited form instead of the base form.

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

This is one way to do the job. It's far from the only way. And important questions like how to organize your objects into a namespace haven't even been touched. But understanding exactly how to save time by creating your own custom base form puts you up the ladder to understanding how to do anything in VB.NET.

article illustrations © Dan Mabbutt

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Create Your Own Custom Form Object

©2008 About.com, a part of The New York Times Company.

All rights reserved.