1

我有一个继承自TreeView. 在此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;
    }
}

我的问题是,当开发人员在 a 上订阅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!?
    }
}

我想做的是禁用AfterCheck事件触发并在我的CustomTreeView班级中自己调用它,这样我就可以传递参数TreeViewAction等于ByMouse。像这样的东西:

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));
    }
}

这可能吗?

4

1 回答 1

1

当然,只需覆盖它OnAfterCheckCustomTreeView它就会像你想要的那样工作。

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   
    }
}

在此处输入图像描述

于 2012-08-22T07:37:03.863 回答