1. Technology

Create Your Own Custom Form Object

Need several forms in your project that are similar, but not exactly the same?

By

Updated August 12, 2007

Using Inherited Forms and the Inheritance Picker

Here's an example problem. You're building this system that has a lot of forms that need a confirmation dialog. Company standards require some extra content in the dialogs, however. The different confirmation dialog forms might look like the ones shown in this illustration:

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

The key thing is that each form has the same background, logo (with included resource file), Confirm and Cancel buttons and a button to display copyright information if requested. They also have unique content that could be pulled from a database or based on some other process in the system.

It wouldn't be too much trouble to simply copy and paste a similar form over and over in the project. The problem with this idea is that it would create a lot of wasted duplication in the project files and maintenance problems down the road. For example, if you needed to change the top button from "Display Copyright Information" to "Display Confidentiality Agreement" it would mean finding and changing each and every confirmation dialog in this project and perhaps a lot of others too.

The short way of saying that is, "It wouldn't be Object Oriented!"

What you need is your own custom form that can be inherited in your project. That's what this article is about.

"Step One" of any effort like this is always design. Before you start programming a form that can be inherited, check out what will be common in each form and what will be variable. Make sure that you know what else the form will need to contain. It's a lot easier to make these decisions now than to change them later.

In the example shown above, the base form will look like this:

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

Now create a form that contains all of the common properties and methods that you want all of the forms to have. In this example, I've added Click events for each of the buttons that simply display a message. (Simple functions that serve as place holders for code that you plan to add later are often called "program stubs".)

The fun part starts on the next page! We use the inheritance picker in Visual Studio to find and inherit the form.

article illustrations © Dan Mabbutt

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. Create Your Own Custom Form Object

©2014 About.com. All rights reserved.