Link to home
Start Free TrialLog in
Avatar of Roger
RogerFlag for United Kingdom of Great Britain and Northern Ireland

asked on

TreeView 6.0 for Excel 2010. How to trap the mouse button that clicks a node AND get the KEY of the node that is clicked?

In Excel 2010, using VB6.0 TreeView (and NOT using .NET) ... in the TreeView control named tv_CI, I can identify the MOUSE BUTTON that clicked a tv_cI.node via:

Private Sub tv_CI_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)
 'my code...
End Sub

To identify the node clicked, I can use:

Private Sub tv_CI_NodeClick(ByVal Node As MSComctlLib.Node)
 'my code...
End Sub

When these procedures are used together in the same UserForm code, _Mousedown takes precedence and _Nodeclicked does not fire.

How can I gain both the Button value and the Node key when the node is clicked?

Thanks
Kelvin
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Roger

ASKER

Thanks, Rgonzo

I tested your code:

Private Sub tv_CI_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)
    Dim PointedNode As Node
    Debug.Print "1"
    Set PointedNode = tv_CI.HitTest(x, y)
    Debug.Print "2"
    MsgBox "£$ PointedNode = " & PointedNode '.Key (I tried with an without .Key)
end sub

It failed at the last line which I inseted (MsgBox....
and on that line it returned a value of PointedNode = Nothing.

However, it did not fail on your code line: Set PointedNode = tv_CI.HitTest(x, y)
I tried click in different positions on the node name, but without success

Can you help further?

Kelvin
Avatar of Rgonzo1971
Rgonzo1971

tried 2nd version?
As a test, add a label to your userform and try this.

Private Sub TreeView1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)
    Dim tvNode As Node
    Set tvNode = TreeView1.HitTest(x * 15, y * 15)
    If Not tvNode Is Nothing Then
        Label1.Top = y + TreeView1.Top
        Label1.Caption = tvNode.Text
    End If

    Set tvNode = Nothing

End Sub

Open in new window

Avatar of Roger

ASKER

Sorry Rgonzo, I must have missed your 2nd version and will address it now and report soon.

Kelvin
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Roger

ASKER

Thanks!
Definately test both ideas in next 5 hours!
Avatar of Roger

ASKER

Two very quick and (for me at least) ingenious solutions.

Rgonzo:  Set nodX = TreeView1.SelectedItem worked a treat with the MouseUp event (Mousedown was no use)
The code was:

Private Sub tv_CI_Mouseup(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)
    Dim tvNode As Node
    Set tvNode = tv_CI.SelectedItem
    MsgBox " tvNode = " & tvNode.Key '.Key.name
End Sub

MartinLiss: In the same events for MouseUp or Down, I did get some transfer of Node name to Label.caption, but it often reported the value for the node adjacent to the node that was clicked, and other times it failed to respond.

Now I need to keep an eye out for rare errors in reporting, because this data must be correct.

I award marks to both since there was no certainty which approach (or any) would actually work.

Very much appreciate your help!!
Kelvin
The key to my code is that x and y in that event are in pixels and most everything else is done it twips, and 1 pixel is 15 twips.

In any case you're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.

Marty - MVP 2009 to 2015
              Experts Exchange MVE 2015
              Experts-Exchange Top Expert Visual Basic Classic 2012 to 2015