C # System.Windows.Forms.TreeView: stop auto-expand / crash on double-click (and execute another handler)

Is there an easy way to disable Auto-expand / Crash TreeView node when double clicked? I could not find an answer to this question that works without checking in BeforeExpand / BeforeCollapse if the current system time matches what was expected for a double click - overriding OnNodeMouseDoubleClick and / or OnDoubleClick does not seem to be enough.

Or, checks the system time and sees if a double click suits him, the only way to do this?

Thanks for your help, -Walt

+4
source share
3 answers

Solved: In fact, the whole solution was http://www.developersdex.com/gurus/code/831.asp . Obviously, OnNodeMouseDoubleClick () is not called in the WM_LBUTTONDBLCLK handler for TreeView in general., It is called in the LBUTTONUP handler. So, the following: on this site:

protected override void DefWndProc(ref Message m) { if (m.Msg == 515) { /* WM_LBUTTONDBLCLK */ } else base.DefWndProc(ref m); } 

If you want to stop processing to the left of the node, then in OnNodeMouseDoubleClick (), follow these steps:

 if (eX >= e.Node.Bounds.Left) { return; } 
+10
source

There was not much luck in the answers I have found so far, but Walt's answer gave inspiration for this:

 int treeX; // somewhere in class scope // Add a MouseMove event handler private void treeView1_MouseMove(object sender, MouseEventArgs e) { treeX = eX; } // Add a BeforeExpand event handler private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e) { if (treeX > e.Node.Bounds.Left) e.Cancel = true; } 
0
source

However, this thread is old ... I did not find an easy solution to this problem, so I myself investigated it. This is the result:

Inherit a specialized Treeview that has the desired behavior from Treeview. Override MouseDown and double check if it will be. If so, prevent expansion / failure by setting a flag to suppress the action. BeforeExpand / collapse are redefined to cancel the action if the flag is set. You can reset the flag in your BeforeExpand / Collapse-EventHandler if you wish.

  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 
0
source

All Articles