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?

Kelvin4
Kelvin4 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Hi,

pls try ( it could be buggy)

    Dim PointedNode As Node
    Set PointedNode = TreeView1.HitTest(x, y)

Open in new window

EDITED or

Set nodX = TreeView1.SelectedItem

Open in new window

Regards

Author

Commented:
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
Top Expert 2016

Commented:
tried 2nd version?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

Author

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

Kelvin
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
And mine?

Author

Commented:
Thanks!
Definately test both ideas in next 5 hours!

Author

Commented:
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
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial