VB Migration Partner

KNOWLEDGE BASE - Forms and controls

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 

Follow Francesco Balena on VB6 migration’s group on


Read Microsoft Corp’s official case study of a VB6 conversion using VB Migration Partner.

Code Architects and its partners offers remote and onsite migration services.

More details

Subscribe to our free newsletter for useful VB6 migration tips and techniques.


To learn more about your VB6 applications, run VB6 Analyzer on your source code and send us the generated text file. You will receive a detailed report about your VB6 applications and how VB Migration Partner can help you to quickly and effectively migrate it to .NET.

Get free advice

A fully-working, time-limited Trial Edition of VB Migration Partner allows you to test it against your actual code

Get the Trial

The price of VB Migration Partner depends on the size of the VB6 application, the type of license, and other factors

Request a quote

Migrating a VB6 application in 10 easy steps

Comparing VB Migration Partner with Upgrade Wizard

Migration tools: Feature Comparison Table

All whitepapers