VB Migration Partner

KNOWLEDGE BASE - Forms and controls

Previous | Index | Next 

[INFO] HitTest method of the TreeView control can return different values in VB.NET

Both the VB6 and the .NET TreeView controls expose the HitTest method, but the two versions of the method work in a slightly different manner. When the mouse hovers to the right of a TreeView element – that is, to the right of the last character of the element’s caption – the VB6 version of the HitTest method assumes that the mouse isn’t on any element and therefore returns Nothing. Conversely, in the same situation the VB.NET version of the method returns a reference to the TreeNode object on the same line.

VB Migration Partner doesn’t automatically account for this minor difference. However, you can easily force VB.NET to behave as VB6 by adding a check on the mouse’s X-coordinate, as in:

        Dim node As VB6Node = TreeView1.HitTest(x,y)
        If node IsNot Nothing AndAlso Me.TextWidth(node.Text) < x Then node = Nothing

You typically add the extra statement by means of an InsertStatement pragma, as in this VB6 code:

        Private Sub TreeView1_MouseMove(Button As Integer, Shift As Integer, _
            x As Single, y As Single)
            Dim node As Node
            Set node = TreeView1.HitTest(x, y)
            '## InsertStatement If node IsNot Nothing AndAlso _
            '## InsertStatement     Me.TextWidth(node.Text) < x Then node = Nothing
            If node Is Nothing Then
                lblMessage.Caption = "NULL"
                lblMessage.Caption = node.Text 
            End If
        End Sub

An important note: this fix assumes that the TreeView control uses the same font as the parent form; if this isn’t the case you must find an alternative way to calculate the width of the node’s caption.

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