VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

You are here: Visual Basic > Advanced VB6 tutorial > Chapter 5

Enabling Two-Tier Validation With the Form's KeyPreview Property in Visual Basic 6

VB forms have KeyPress, KeyUp, and KeyDown events with behavior exactly like that of the same events for individual controls.

By default, a form's three Keystroke events are not enabled. Even if you wrote code in the event procedures of Form_KeyPress, Form_KeyUp, or Form_KeyDown, nothing would happen. In order to enable these form events, you must set the form's KeyPreview property to True (its default value is False).

Once you've set the form's KeyPreview property to True and placed code in its Keystroke events, you have enabled two-tier keyboard validation. In other words, keyboard information will pass through two successive sets of validation routines: the form's Keystroke routines and then the keystroke routines for the control that currently has the focus.

When KeyPreview is True, the form's Keystroke events will run first. If the form's Keystroke event procedures modify the keystroke information, then the control's Keystroke events will receive that modified information and not the original keystroke information.

So, for example, if a form's KeyPress event procedure changes all characters to uppercase, then no control on the form will ever see a lowercase character in its KeyPress event.

Use common sense to decide whether to put keystroke validation code at the form or the control level. If you need some validation to take place for all keystrokes on the entire form (e.g., all characters in all fields need to be uppercase), then that validation should happen in the form-level Keystroke event procedures. Control-specific validation should happen in control Keystroke event procedures.

Keystroke Events at Field and Form Level topics

See Also


<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved