如何右键单击树视图控件中选择节点

发布于 2024-12-02 19:03:45 字数 66 浏览 0 评论 0原文

为什么我右键单击树视图中的节点,焦点会移至该节点,然后立即返回到先前选择的节点。有什么方法可以允许右键单击选择节点吗?

Why I right click on a node in my treeview the focus moves to this node and then immediately back to the previously selected node. Is there some way that I can allow the right click to select the node?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为守护你 2024-12-09 19:03:45

这是因为突出显示颜色执行两个职责,它显示选定的节点显示聚焦的节点。如果您不对右键单击事件执行任何操作,则会跳回到所选节点。解决方法是选择节点:

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
        if (e.Button == MouseButtons.Right) treeView1.SelectedNode = e.Node;
    }

加上您想要执行的任何其他操作,通常显示上下文菜单。

That's because the highlight color performs two duties, it shows the selected node and shows the focused node. If you don't do anything with the right-click event then it jumps back to the selected node. The workaround is to select the node:

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
        if (e.Button == MouseButtons.Right) treeView1.SelectedNode = e.Node;
    }

Plus anything else you want to do, usually displaying a context menu.

池木 2024-12-09 19:03:45

抱歉,我有点操之过急,我发现如何做到这一点如下:

    Private Sub TreeView1_NodeMouseClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            TreeView1.SelectedNode = e.Node
        End If
    End Sub

控件不应该按照标准执行此操作吗?

Apologies I jumped the gun slightly I found how to do this as follows:

    Private Sub TreeView1_NodeMouseClick(sender As Object, e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            TreeView1.SelectedNode = e.Node
        End If
    End Sub

Shouldn't the control do this as standard though?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文