C# System.Windows.Forms.TreeView:双击时停止自动展开/折叠(并执行另一个处理程序)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1307891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# System.Windows.Forms.TreeView: Stop auto expand/collapse on double-click (and execute another handler)
提问by Walt W
Is there a simpleway 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.
有没有一种简单的方法可以在双击时禁用 TreeView 节点的自动展开/折叠?如果当前系统时间与预期的双击匹配,我一直无法找到这个问题的答案,如果不检查 BeforeExpand/BeforeCollapse,覆盖 OnNodeMouseDoubleClick 和/或 OnDoubleClick 似乎还不够。
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
感谢您的帮助,-沃尔特
回答by Walt W
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:
已解决:实际上,整个解决方案位于http://www.developersdex.com/gurus/code/831.asp。显然,在 TreeView 的 WM_LBUTTONDBLCLK 处理程序中根本没有调用 OnNodeMouseDoubleClick()。. . 它在 LBUTTONUP 处理程序中调用。因此,以下是该站点的内容:
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:
如果要停止对节点左侧的处理,则在 OnNodeMouseDoubleClick() 中执行以下操作:
if (e.X >= e.Node.Bounds.Left) {
return;
}
回答by Qodex
Haven't had much luck with any of the answers I've found so far, but Walt's answer provided the inspiration for this:
到目前为止我找到的任何答案都没有多少运气,但 Walt 的答案为此提供了灵感:
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;
}
回答by Frank Wolter
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.
从 Treeview 继承具有所需行为的专用 Treeview。覆盖 MouseDown 并检查它是否是双击。如果是这样,请通过设置一个标志来阻止操作来防止扩展/折叠。如果设置了标志,则覆盖 BeforeExpand/collapse 以取消操作。如果需要,您可以在 BeforeExpand/Collapse-EventHandler 中重置标志。
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