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