VB.NET code to prevent Expand() and Collapse() from firing when TreeView is doubleclicked.
Add this class to your project:
And use it in your Form like this:
TreeView is an annoying piece of ****
I got the idea from here:
http://www.windowsforms.net/Forums/ShowPost.aspx?tabIndex=1&tabId=41&PostID=15689
Add this class to your project:
Code:
'DoubleClick-expand restriction hack
Public Class cTreeViewDblClickHack
Private WithEvents m_tv As TreeView
Public Sub New(ByVal tv As TreeView)
m_tv = tv
End Sub
Private m_FirstMouseDownTime As Long
Private blnDoubleClick As Boolean = False
Private Sub tv_BeforeExpand(ByVal sender As Object, _
ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) _
Handles m_tv.BeforeExpand
If blnDoubleClick AndAlso e.Action = TreeViewAction.Expand Then
e.Cancel = True
Else
m_FirstMouseDownTime = 0
End If
End Sub
Private Sub tv_BeforeCollapse(ByVal sender As Object, _
ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) _
Handles m_tv.BeforeCollapse
If blnDoubleClick AndAlso e.Action = TreeViewAction.Collapse Then
e.Cancel = True
Else
m_FirstMouseDownTime = 0
End If
End Sub
Private Sub tv_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles m_tv.MouseDown
If Now.Ticks - m_FirstMouseDownTime <= (SystemInformation.DoubleClickTime * TimeSpan.TicksPerMillisecond) Then
blnDoubleClick = True
Else
blnDoubleClick = False
End If
m_FirstMouseDownTime = Now.Ticks
End Sub
End Class
And use it in your Form like this:
Code:
Private m_TreeViewDblClickHack As cTreeViewDblClickHack
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
m_TreeViewDblClickHack = New cTreeViewDblClickHack(Me.tv)
End Sub
TreeView is an annoying piece of ****
I got the idea from here:
http://www.windowsforms.net/Forums/ShowPost.aspx?tabIndex=1&tabId=41&PostID=15689
Last edited: