Reputation: 3349
Is there a simple way to disable the auto-expand/collapse of a TreeView node when it is double-clicked? I've been unable to find an answer to this question that works without checking in BeforeExpand/BeforeCollapse if the current system time matches that expected for a double-click - overriding OnNodeMouseDoubleClick and/or OnDoubleClick does not seem to suffice.
Or, is checking the system time and seeing if it fits a double-click the only way to do this?
Thanks for your help, -Walt
Upvotes: 5
Views: 10273
Reputation: 1
Nevertheless this thread is old... I didn't find an easy solution for this problem, so I investigated on my own. This is the result:
Inherit a specialized Treeview which has the desired behaviour from Treeview. Overriding the MouseDown and checking if it will be a doubleclick. If so, prevent expansion/collapse by setting a flag to supress the action. BeforeExpand/collapse are overridden to cancel the action if the flag is set. You could reset the flag in your BeforeExpand/Collapse-EventHandler if you want to.
Public Class DblClickTreeview
Inherits TreeView
Private _SupressExpColl As Boolean = False
Private _LastClick As DateTime = Now
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
_SupressExpColl = Now.Subtract(_LastClick).TotalMilliseconds <= SystemInformation.DoubleClickTime
_LastClick = Now
MyBase.OnMouseDown(e)
End Sub
Protected Overrides Sub OnBeforeCollapse(e As TreeViewCancelEventArgs)
e.Cancel = _SupressExpColl
MyBase.OnBeforeCollapse(e)
End Sub
Protected Overrides Sub OnBeforeExpand(e As TreeViewCancelEventArgs)
e.Cancel = _SupressExpColl
MyBase.OnBeforeExpand(e)
End Sub
End Class
Upvotes: 0
Reputation: 329
Haven't had much luck with any of the answers I've found so far, but Walt's answer provided the inspiration for this:
int treeX; // somewhere in class scope
// Add a MouseMove event handler
private void treeView1_MouseMove(object sender, MouseEventArgs e)
{
treeX = e.X;
}
// Add a BeforeExpand event handler
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
if (treeX > e.Node.Bounds.Left) e.Cancel = true;
}
Upvotes: 0
Reputation: 3349
Solved: Actually, the entire solution was at http://www.developersdex.com/gurus/code/831.asp . Apparently OnNodeMouseDoubleClick() is not called in the WM_LBUTTONDBLCLK handler for TreeView at all . . . it's called in the LBUTTONUP handler. So, The following is what's at that site:
protected override void DefWndProc(ref Message m) {
if (m.Msg == 515) { /* WM_LBUTTONDBLCLK */
}
else
base.DefWndProc(ref m);
}
If you want to halt handling to the left of the node, then in OnNodeMouseDoubleClick() do the following:
if (e.X >= e.Node.Bounds.Left) {
return;
}
Upvotes: 15