[英]Prevent custom control event from firing
I have a custom control that inherits from TreeView
. 我有一个自定义控件,它继承自
TreeView
。 In this CustomTreeView
, I handle the OnNodeMouseClick
event to perform some process before changing the node.Checked
state as the user would expect it: 在此
CustomTreeView
,我处理OnNodeMouseClick
事件以在更改node.Checked
状态之前执行某些过程,这是用户期望的状态:
public class CustomTreeView : TreeView {
// Constructor...
protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e) {
base.OnNodeMouseClick(e);
// Do something...
e.Node.Checked = !e.Node.Checked;
}
}
My problem is when the developer subscribes to the AfterCheck
event on a CustomTreeView
, the value of e.Action
is always TreeViewAction.Unknown
(because the checked state of the node is changed in the code), whereas the developer is waiting for TreeViewAction.ByMouse
: 我的问题是,当开发商签约的
AfterCheck
在事件CustomTreeView
,价值e.Action
总是TreeViewAction.Unknown
(因为节点的选中状态中的代码改变),而开发商在等待TreeViewAction.ByMouse
:
public partial class Form1: Form {
private void customTreeView1_AfterCheck(object sender, TreeViewEventArgs e) {
// e.Action == TreeViewAction.Unknown
// [developer] The user clicked on the node, it should be
// TreeViewAction.ByMouse!?
}
}
What I would like to do is disable the AfterCheck
event from firing and call it myself in my CustomTreeView
class, that way I would be able to pass parameters with TreeViewAction
equal to ByMouse
. 我想做的是禁用
AfterCheck
事件,并在CustomTreeView
类中自己调用它,这样我就可以传递TreeViewAction
等于ByMouse
参数。 Something like that: 像这样:
public class CustomTreeView : TreeView {
// Constructor...
protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e) {
base.OnNodeMouseClick(e);
// Do something...
// Prevent all AfterCheck events from firing, just for a moment
// ??
e.Node.Checked = !e.Node.Checked;
// Allow AfterCheck events to fire
// ??
// Call myself the AfterCheck event
base.OnAfterCheck(new TreeViewEventArgs(e.Node, TreeViewAction.ByMouse));
}
}
Is this possible? 这可能吗?
Sure, just override OnAfterCheck
in CustomTreeView
and it will work like you intend. 当然,只需在
CustomTreeView
覆盖OnAfterCheck
,它便会按您的CustomTreeView
工作。
public class CustomTreeView : TreeView {
protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e) {
base.OnNodeMouseClick(e);
// your stuff
// Call myself the AfterCheck event
base.OnAfterCheck(new TreeViewEventArgs(e.Node, TreeViewAction.ByMouse));
}
protected override void OnAfterCheck(TreeViewEventArgs e)
{
// do nothing
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.