Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Give Your Forms a Base : Page 2

A base form class makes it easy for you to give your users a consistent experience without needing to write a lot of user interface code.


advertisement
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.

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.



Deborah Kurata is cofounder of InStep Technologies Inc., a professional consulting firm that focuses on turning your business vision into reality using Microsoft .NET technologies. She has over 15 years of experience in architecting, designing, and developing successful .NET applications. Deborah is the author of several books, including "Doing Objects in Visual Basic 6.0" (SAMS) and "Doing Web Development: Client-Side Techniques" (APress). She is on the INETA Speaker's Bureau and is a well-known speaker at technical conferences.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date