Previous Page
Next Page

Hack 27. Highlight the Active Control

Provide a visual aid on a busy form by emphasizing the control that has the focus.

As you tab through a form and enter data, it is possible to get lost. The more controls on a form, the more likely this is to occur. This hack shows you how to keep the current entry box highlighted.

"Be Tab-Smart" [Hack #26] shows a way in which users can tab around the form in a sensible manner. That hack works with a little code put into a control's Exit event. This hack uses the Exit event, as well as the Enter event. In this hack, all controls that accept entry have code in both events.

Figure 3-25 shows the entry form with a border around the First Name entry box. As a user tabs through the form or clicks specific controls, the border will always appear around the active control. The border is displayed on only one control at a time.

Figure 3-25. A border appearing around the active control

To get this to work, the controls on the form must have their Special Effect property set to Flat. You can use other settings that allow borders to be displayed, such as Shadow, but Flat seems to work best. When Shadow is used, and when the control loses the border, the control still retains the shadow part of the border, and that doesn't look right.

Figure 3-26 shows the code module behind the form. Two subroutines are at the top: active_control_enterand active_control_exit. These subroutines run when they're called by each control's Enter and Exit events. The active_ control_enter subroutine sets the active control, whatever it is at the time, to have a thick, red border. The border style value of 1 indicates a solid border. The border width value of 2 indicates a thick border. Red is then set as the border color.

The active_control_exit subroutine has just a single line of code; it sets the border to transparent.

Figure 3-26. The code for turning a border on and off

Each time a control is entered, it means a different control was just exited. Therefore, these two subroutines fire in a pair. The Exit routine turns off the border for one control, and the Enter routine turns it on for another.

Using the technique in this hack ensures that users clearly see which control is active.

    Previous Page
    Next Page