Create a base form class to ensure that all of the forms in your application behave consistently.

This technique minimizes the amount of repetitive code you need to write to manage the user's interaction with your forms.

Great Windows applications have a consistent user interface. As the user learns how to work with one form in the application, the user can leverage that knowledge as they work with other forms in the application. This minimizes the requirements for end-user training and support and maximizes end-user satisfaction.

Add event handlers to the base form class to handle form and control events consistently on any form that inherits from the base form class.

You can achieve a consistent feel throughout your user interface by building your own base form class. In this base form class you can write the code for all of the standard user interaction features. Every form in the application that inherits from your base form class will automatically support any feature defined in the base form class.

Creating the Base Form Class

You create a base form class the same way you create any form in a Windows application; just add a new form to a Windows Application project. Giving the base form class a clear name, such as eOrderOnLineWinBase, will help you keep track of the base form class.

You may, or may not, want to add controls to this base form class. If there are common UI elements on many of the forms of your application, such as a particular icon or set of buttons, then it is useful to add these to the base form class. Common UI elements in the base form class then appear on every form that inherits from the base form class, a technique referred to as visual inheritance. Frequently, however, you may find that your forms don't have common UI elements, so it is not desirable to add common controls in fixed positions to your base form class.

You should add event handlers to the base form class to handle form and control events consistently on any form that inherits from the base form class. By leveraging the code in the base form, you minimize the amount of repetitive code in the forms.

For example, to give the user a visual indicator of the control that has focus, you can change the background color of the active control on the form. To be consistent, every form should incorporate the same technique. You could add the code to handle the Enter and Leave events of every control to every form in your application; but this could require a significant amount of code since a Windows application of any significant size can require 50 or more forms. It makes more sense to write this code one time and then reuse it from every form in your application.

Defining the Event Handlers

To begin, create ProcessEnter and ProcessLeave event handlers for your new base form class. The ProcessEnter handler changes the background color of a textbox to a wheat color (light tan) and the ProcessLeave handler changes the background color back to the standard Window color. You can modify these handlers to process other types of controls as well.

In VB.NET:

Private Sub ProcessEnter(ByVal sender As Object, _
              ByVal e As System.EventArgs)
   DirectCast(sender, TextBox).BackColor = _
                                       Color.Wheat
End Sub
Private Sub ProcessLeave(ByVal sender As Object, _
               ByVal e As System.EventArgs)
   DirectCast(sender, TextBox).BackColor = _
           Color.FromKnownColor(KnownColor.Window)
End Sub

In C#:

private void ProcessEnter(object sender,
               System.EventArgs e)
{
    ((TextBox)sender).BackColor = Color.Wheat;
}
private void ProcessLeave(object sender,
               System.EventArgs e)
{
    ((TextBox)sender).BackColor =
   Color.FromKnownColor(KnownColor.Window);
}

Next, you need a way to "wire up" all of the textbox controls on a form to the ProcessEnter and ProcessLeave events when your application loads your form. You could do this by adding code to the Load event on each form, but adding it to the base form class means that you don't need to repeat the code. In addition, as you add forms to the application, you don't need to remember to wire up the events.

To inherit from the base form class from any form in your application, you simply change the class declaration for the form.

The base form loads automatically when an inherited form loads. So the best place to put the code to "wire up" the event handler is in the Load event of the base form class:

In VB.NET:

Private Sub eOrderOnLineWinBase_Load(_
     ByVal sender As System.Object, _
     ByVal e As System.EventArgs) _
     Handles MyBase.Load
   SetEventHandlers(Me)
End Sub

In C#:

private void eOrderOnLineWinBase_Load(object
         sender, System.EventArgs e)
{
SetEventHandlers(this);
}

In this snippet, Me (VB.NET) or this (C#) refers to the currently running instance, which is the instance of the form that inherits from this base form class, commonly called a child form.

The SetEventHandlers method iterates through the Controls collection of the child form, which is no easy task since the Controls collection is hierarchical. The first level of the Controls collection hierarchy includes only the controls directly on the form. The second level of the hierarchy includes the controls that are contained in any of the controls that are directly on the form.

An example may clarify this concept. Say you have a form with two panels on it: pnlSelection and pnlInformation. In pnlSelection you have a combo box that allows the user to select an entry. In pnlInformation you have a tab control. The tab control contains two tab pages and on each tab page you have sets of controls that include textbox controls. If you looked at the Controls collection for the form you would see that the collection only contains two controls: pnlSelection and pnlInformation. These are the only two controls in this example that are directly on the form.

To access the controls contained in pnlSelection, you have to access the Controls collection for pnlSelection. To access the controls on the tab control on pnlInformation, you have to access the Controls collection for pnlInformation (which contains the tab control), the Controls collection for the tab control (which contains the tab pages), and then the Controls collection of each of the tab pages (which contains the text boxes).

To write a function that accesses all of the controls on the form in a generic fashion, you need to write a function that is recursive, meaning that it calls itself. The function iterates through the Controls collection and, if it finds a control in the Controls collection that contains other controls (referred to as child controls), the function will call itself to iterate through the child control's Controls collection.

In VB.NET:

Private Sub SetEventHandlers(ByVal ctrlContainer _
As Control)
  Dim ctrl As Control
  For Each ctrl In ctrlContainer.Controls
    If TypeOf ctrl Is TextBox Then
      AddHandler ctrl.Enter, _
                           AddressOf ProcessEnter
      AddHandler ctrl.Leave, _
                           AddressOf ProcessLeave
    End If
    If ctrl.HasChildren Then
      SetEventHandlers(ctrl)
    End If
  Next
End Sub

In C#:

private void SetEventHandlers(Control
            ctrlContainer)
{
  foreach (Control ctrl in ctrlContainer.Controls)
  {
if (ctrl is TextBox)
   {
      ctrl.Enter +=
new System.EventHandler(this.ProcessEnter);
      ctrl.Leave +=
new System.EventHandler(this.ProcessLeave);
   }
      if (ctrl.HasChildren)
   {
      SetEventHandlers(ctrl);
   }
  }
}

This routine sets the Enter and Leave event handlers for all of the textbox fields of the form, even if the textbox is contained within another control. The AddHandler statement (VB.NET) or EventHandler (C#) defines the event handler to call when the event occurs. In this example, the code will call the ProcessEnter method when a textbox generates an Enter event (the user enters a control) and the ProcessLeave method when a textbox generates a Leave event (the user leaves a control).

You can add any other event handlers to this base form class. For example, you could handle the Initialize event for a grid to set up a standard grid look (border colors and styles, header colors and styles, and so on). You could even handle the Validating event to perform standardized validation for the controls on all of the forms in the application.

Inheriting from the Base Form Class

The only remaining step involves using the base form class in any form that needs the standard user interface behavior.

To inherit from the base form class from any form in your application, you simply change the class declaration for the form as follows:

In VB.NET:

Public Class LoginWin
    Inherits eOrderOnLineWinBase

In C#:

public class LoginWin : eOrderOnLineWinBase

When the user enters a textbox on any form that inherits from eOrderOnlineWinBase, the form will change the background color to a wheat color (light tan) allowing the user to easily see which control has focus. When the user leaves the text box, the form will change the background color back to the user's default Windows color.

Conclusion

It is very tedious to write event procedure and other user-interface code in your Windows applications, especially when you have many forms in your application. By using a base form class you can write that code one time and leverage it from all of the forms in your application.

The technique described in this article provides a base form class that any form can inherit, giving your users a consistent experience and minimizing the code that you need to write.