TreeView NodeMouseClick - 如何判断点击的节点部分

Jam*_* R. 0 .net c# winforms

我有一个TreeView和ShowPlusMinus设置为true.单击文本时,我还希望节点(仅父项)切换.这是我的NodeMouseClick事件处理程序:

    private void NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Node.GetNodeCount(true) == 0)
        {
            if (MapClick != null) MapClick(this, e); // fire an event that a child was clicked
        }
        else
        {
            e.Node.Toggle();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我无法使用AfterSelect,因为即使已经选择了节点,我也需要切换.

NodeMouseClick的问题在于,当用户单击加号/减号图标时也会调用它.所以节点切换两次.我需要阻止默认行为并且总是自己切换,或者我需要检测项目被点击的位置,并且只有当它不在正/负时才进行切换.

Han*_*ant 9

使用TreeView.HitTest()方法来发现单击的部分.像这样的东西(省略你的逻辑,因为我无法理解它):

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
        var hit = treeView1.HitTest(e.Location);
        if (hit.Location == TreeViewHitTestLocations.Label) {
            // etc..
        }
    }
Run Code Online (Sandbox Code Playgroud)