Event Sequences in Visual Basic VB6
|Action||Events triggered (in order)|
|form load|| Form_Load|
|form restore from icon||Form_Paint (note: no Form_Resize)|
|form maximize|| Form_Resize|
Form_MouseMove (note: even with no mouse movement!)
|form restore from max||Form_Resize|
|form move by drag||Form_MouseMove...|
|form resize by drag|| Form_MouseMove...|
Form_MouseMove... (note 2nd 'relative' move)
|form close|| Form_QueryUnload|
|click in text box|| Text1_MouseMove... (only when Text1.Enabled is True)|
(sets Text1.SelStart BEFORE MouseDown event)
Text1_MouseDown (note: before LostFocus!)
|single character in text box|| Text1_MouseMove...|
Form_KeyDown (if Form.KeyPreview is True)
Form_KeyPress (if Form.KeyPreview is True)
Form_KeyUp (if Form.KeyPreview is True)
|combo box set by listindex||Combo1_Click (note: no Change event!)|
|single character in combo box|| Combo1_GotFocus|
|combo box select|| Combo1_DropDown (note: before GotFocus!)|
Combo1_Click (note: no Change event!)
|click on grid cell|| Grid1_RowColChange (note: before GotFocus!)|
|click on Command1 while in Text1|| Form_MouseMove...|
Command1_MouseDown (note: before LostFocus!)
Command1_Click (note: before MouseUp!)
|tab from Text1 to Text2|| Text1_LostFocus (note: LostFocus before GotFocus)|
- When you click on a control other than the current one, the MouseDown event happens BEFORE the previous control's LostFocus event, and BEFORE the new control's GotFocus event. One valuable use of this is that you can detect the user clicking the Cancel button and programatically avoid running data validation in the LostFocus event of whatever field they were in before clicking Cancel.
- The Click event for a Command button happens BEFORE the MouseUp event, while for most other controls, it happens AFTER the MouseUp event.
- A number of controls have events that happen BEFORE the GotFocus event. For example, you can build or adjust the list of a ComboBox in the DropDown event before GotFocus. In the Grid control, you get a RowColChange event before the GotFocus event.
- When you click in a TextBox, the SelStart property is set (based on the position of the mouse) before ANY other event takes place.
New Validation Event in VB6
Visual Basic 6.0 added a new Validate event and CausesValidation property to aid in field-by-field validation. To use the new Validate event, set the CausesValidation property to True for each field that could receive focus, which luckily is the default value for that property. As you leave a field, the Validate event is then generated before any of the focus events (such as LostFocus) are generated. This allows you to validate the data before actually losing focus from the field.
If you want to have a field on the form that the user can select without going through any validation, you can set the CausesValidation property to False for that field. For example, if you have a Help or Cancel button, you want to allow the user to select the button without having the current field validated. So set the CausesValidation property for the Help or Cancel button to False. When the user leaves a field and clicks the Help button, the user will be able to view the help without first validating the field.
The Validate event has a Cancel parameter. You can set this parameter to True if you want to keep focus on the field that is in error. Alternatively, you can set the Cancel parameter to False (the default) if you want to allow the end-user to continue entering data without immediately correcting the problem.
You may want to use the Validate event without using the Cancel parameter. If an error occurs during validation, you can set the field’s Forecolor property to red and modify the ToolTipText property to display the specific error message for that field. That tells the user something is wrong without interrupting the data entry flow.