I have a custom control that inherits from 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:
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:
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. 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
OnAfterCheckinCustomTreeViewand it will work like you intend.