Odinsonnah
Odinsonnah

Reputation: 73

VB.Net TreeView NodeMouseClick middle mouse button

I have been using the NodeMouseClick event to handle left and right clicks on my TreeNodes for a while. Now I want to add an effect to middle clicking as well, but the NodeMouseClick event doesn't seem to fire on a middle click. Is this a known bug, or should it work fine and I'm just doing something wrong? If it is a bug (or just intended to function this way), how can I make a middle click on a TreeNode do something specific with that node?

Here's a bit of my code:

Private Sub TreeView1_NodeMouseClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
    If e.Button = Windows.Forms.MouseButtons.Left Then
        Call nodeLeft(e.Node)
    ElseIf e.Button = Windows.Forms.MouseButtons.Middle Then
        Call nodeMiddle(e.Node)
    ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
        Call nodeRight(e.Node)
    End If
End Sub

Upvotes: 0

Views: 1817

Answers (1)

LarsTech
LarsTech

Reputation: 81675

You can try this version:

Public Class MyTreeView
  Inherits TreeView

  Private Const WM_MBUTTONDOWN As Integer = &H207

  Protected Overrides Sub WndProc(ByRef m As Message)
    MyBase.WndProc(m)
    If m.Msg = WM_MBUTTONDOWN Then
      Dim p As Point = Me.PointToClient(MousePosition)
      Dim mouseNode As TreeNode = Me.GetNodeAt(p)
      If mouseNode IsNot Nothing Then
        Me.OnNodeMouseClick(New TreeNodeMouseClickEventArgs(mouseNode, MouseButtons.Middle, 1, p.X, p.Y))
      End If
    End If
  End Sub
End Class

It will fire the NodeMouseClick event with the middle value set for the Button property. It won't select the node though. To do that, add the line Me.SelectedNode = mouseNode above the OnNodeMouseClick call.

Upvotes: 1

Related Questions