HitTest Method (ListView, TreeView Controls)

           

Returns a reference to the ListItem object or Node object located at the coordinates of x and y. Most often used with drag-and-drop operations to determine if a drop target item is available at the present location.

Syntax

object.HitTest (x As Single, y As Single)

The HitTest method syntax has these parts:

Part Description
object An object expression that evaluates to an object in the Applies To list.
x,y Coordinates of a target object, which is either a Node object or a ListItem object.

Remarks

If no object exists at the specified coordinates, the HitTest method returns Nothing.

The HitTest method is most frequently used with the DropHighlight property to highlight an object as the mouse is dragged over it. The DropHighlight property requires a reference to a specific object that is to be highlighted. In order to determine that object, the HitTest method is used in combination with an event that returns x and y coordinates, such as the DragOver event, as follows:

Private Sub TreeView1_DragOver _
(Source As Control, X As Single, Y As Single, State As Integer)
   Set TreeView1.DropHighlight = TreeView1.HitTest(X,Y)
End Sub

Subsequently, you can use the DropHighlight property in the DragDrop event to return a reference to the last object the source control was dropped over, as shown in the following code:

Private Sub TreeView1_DragDrop _
(Source As Control, x As Single, y As Single)
   ' DropHighlight returns a reference to object drop occurred over.
   Me.Caption = TreeView1.DropHighlight.Text 
   ' To release the DropHighlight reference, set it to Nothing.
   Set TreeView1.DropHighlight = Nothing
End Sub

Note in the preceding example that the DropHighlight property is set to Nothing after the procedure is completed. This must be done to release the highlight effect.