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
Kelvin4Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kelvin4Author 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
Rgonzo1971Commented:
tried 2nd version?
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Martin LissOlder than dirtCommented:
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

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

Kelvin
Martin LissOlder than dirtCommented:
And mine?
Kelvin4Author Commented:
Thanks!
Definately test both ideas in next 5 hours!
Kelvin4Author 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 dirtCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.