Previous | Index | Next 

[HOWTO] Implementing the Enter/Leave event handlers for a control array

Starting with version 1.50, the VB6ControlArray class exposes two new events, Enter and Leave, which fire when an element of the array receives or loses the input focus. You can take advantage of these new events by decorating the control array declaration with the WithEvents keyword:

    ' this code is stored in the code-behind portion of a form
    Private WithEvents txtFields As VB6ControlArray(Of VB6TextBox)

and then writing code such as this

    Private Sub txtFields_Enter(ByRef index As Short) Handles txtFields.Enter
    	' handle the Enter for txtFields(index)
    End Sub

    Private Sub txtFields_Leave(ByRef index As Short) Handles txtFields.Leave
         ' handle the Leave for txtFields(index)
    End Sub

Even better, you can use pragmas to add the WithEvents keyword and “transform” the GotFocus and LostFocus event handlers into hanlders of the new Enter/Leave events:

    '## PostProcess "Public (?<ctrl>\w+) As CodeArchitects\.VB6Library\.VB6ControlArray",
          "Public WithEvents ${ctrl} As VB6ControlArray"
    '## PostProcess "Private Sub (?<ctrl>\w+)_GotFocus\(ByRef index As Short\)",
          "Private Sub ${ctrl}_Enter(ByRef index As Short) Handles ${ctrl}.Enter"
    '## PostProcess "Private Sub (?<ctrl>\w+)_LostFocus\(ByRef index As Short\)",
          "Private Sub ${ctrl}_Leave(ByRef index As Short) Handles ${ctrl}.Leave"

Please notice that the new Enter/Leave events “bypass” the standard event mechanism offered by the VB6Library: they are “pure” .NET events that can fire in any moment, including when the form is being loaded or a message box is being showed. When using these new events you should be prepared to handle such minor differences.


Previous | Index | Next