KnowDotNet Visual Organizer

Reusability - Visual PolyMorphism, Taking a Different Look

Resuse Multi-Purpose Forms to Conserve Resources

by Les Smith
Print this Article Discuss in Forums

How can I reuse forms for multiple purposes so that I don't have so many forms that perform much of the same functionality and have many of the same controls?

Every project of any size has numerous dialog type forms, used for data entry.  Many times, the forms use a mix of similiar controls with some new ones for the various functionalities required in the form.

I have started using an Enum to enumerate the various types of functionality that I want the form to perform, and then, based on the functionality required, the same form is reused and takes on a different look and feel.  Not only does this reduce the overall number of forms that I need, but it makes the work of the IDE easier and faster.  As you know, VB.NET is constantly doing a "background compile", which is made slower each time you add new classes and forms to your project.  

This process can be greatly improved by keeping the number of windows that you have open at any one time to a minimum.  I believe this will reduce the number of windows that the IDE is concerned with having to worry about compiling.  This compile is done so that IntelliSense is always up to date.  If you are working in VB.NET, you may have noticed, that as the project size increases, the performance of the IDE, with respect to intellisense, begins to degrade.  Again, I believe that you can help this situation by doing two things; first, reuse classes and forms, by adding additional functionality to them, and keep the number of open windows to a minimum.

In this article, I will show you a simple technique that I have adopted for creating "visual polymorphism".  That's a fifty cent term for making a form take on a different look and feel, based on a parameter that I set when I instantiate it.

First, in the form itself, I will set up an Enum and a usage or feature property that will tell the form how I want it to look and behave when it is shown.  The code for that is shown below.

   Public Enum FormUsage
      SelectList
      MemoFieldUpdate
  
End Enum
   Public UseType As FormUsage = FormUsage.SelectList

I have some forms in my projects that will have five or more different functionalities that it can perform.  That means, among other things, that the form will take on five different appearances when it is shown.  Most of the time, the differences will be more subtle than the one illustrated in this article, but this is simply a test form that I created for this article and it does nothing more useful than to illustrate the technique that I am proposing.  The FormUsage Enum would contain as many enumerations as features that I need the form to provide.

The code shown below will instantiate an instance of the form, and the form will take on the appropriate look and feel.  In this case, the form is going to present a couple of check boxes and a listview, in addition to a couple of common controls that will be present on any instance of the form.

      Dim f As New frmTest
      f.UseType = frmTest.FormUsage.SelectList
      f.ShowDialog()

When the form loads, it will have the appearance as shown below.

Visual PolyMorphism1


In the following code, I am telling the form to take on a different look and feel.

      f.UseType = frmTest.FormUsage.MemoFieldUpdate
      f.ShowDialog()

As shown below, the same form has some of its original controls and some new ones.

Visual PolyMorphism2


In the Form_Load Event of the form, the following code determines the appearance that the form will assume.

   Private Sub frmTest_Load(ByVal sender As Object, _
      
ByVal e As System.EventArgs) Handles MyBase.Load
      
' Determine the look of the form at load time
      ' based on the selected usage.
      With Me
         Select Case UseType
            
Case FormUsage.SelectList
               .grpBox1.Visible =
True
               .grpBox2.Visible = False
               .txtCode.Visible = False
               .lstItems.Visible = True
               .lblText.Text = "Select Item"
               .Text = "Select Item"
            Case FormUsage.MemoFieldUpdate
               .grpBox1.Visible =
False
               .grpBox2.Visible = True
               .txtCode.Visible = True
               .lstItems.Visible = False
               .lblText.Text = "Enter Text"
               .Text = "Enter Memo Data"
         End Select
      End With
   End Sub

Obviously, the code shown above only determines the look and feel of the form at initial load.  There will have to be other code in the form, in the Save and Cancel button events, for example, that will determine the functionality performed based on the FormUsage enumeration.

The whole object of this article is to stress the importance of reusability, which in turn improves performance of the Visual Studio .NET IDE, cuts down the overall project size and resulting maintenance.  As you begin to use this mind set, you will find that you are not only reusing controls and forms, but you will begin to attain greater reusability of the code behind the forms and their associated classes.  That's one of the major advantages of object-oriented programming
.

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing