树节点右键单击选项

发布于 2024-08-05 23:58:06 字数 185 浏览 9 评论 0原文

我正在 C# GUI 应用程序中使用 TreeView 和 TreeView.Nodes,并希望在树中的几个节点上使用右键单击功能。我已经搜索了很多,但似乎 SelectedNode 仅对左键单击有效,并且没有任何内容可以捕获节点上的右键单击。我想在右键单击时向节点添加“添加”、“删除”、“重命名”等功能。请问有什么指导吗?

谢谢, 维伦

I am working TreeView and TreeView.Nodes in my C# GUI application and want to use the right click functionality on a few nodes in my tree. I have searched quite a bit but it seems like the SelectedNode is valid only for left click and there is nothing to capture the right click on a node. I want to add functionalities like 'Add', 'Remove', 'Rename', etc. to the nodes when right clicked upon. Any guidance please?

Thanks,
Viren

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

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

发布评论

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

评论(2

栖迟 2024-08-12 23:58:06

添加 MouseUp 的处理程序。
在处理程序中,检查鼠标右键的参数,如果不是则返回。
使用鼠标坐标调用 treeView.GetNodeAt() 来查找节点。
创建上下文菜单。

对于可适用于 TreeView 的列表控件,这里有类似的内容:

        private void listJobs_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int index = listJobs.IndexFromPoint(e.Location);
                if (index != ListBox.NoMatches)
                {
                    listJobs.SelectedIndex = index;

                    Job job = (Job)listJobs.Items[index];

                    ContextMenu cm = new ContextMenu();


                    AddMenuItem(cm, "Run", QueueForRun, job).Enabled = !job.Pending;
                    AddMenuItem(cm, "Cancel run", CancelQueueForRun, job).Enabled = (job.State == JobState.Pending || job.State == JobState.Running);
                    AddMenuItem(cm, "Open folder", OpenFolder, job);

                    cm.Show(listJobs, e.Location);
                }
            }
        }

        private MenuItem AddMenuItem(ContextMenu cm, string text, EventHandler handler,     object context)
        {
            MenuItem item = new MenuItem(text, handler);
            item.Tag = context;
            cm.MenuItems.Add(item);
            return item;
        }

您可能需要在表单上使用 PointToClient 或 PointToScreen 来适当地转换坐标。当上下文菜单出现在错误的位置时,您很快就会意识到是否需要它们。

Add a handler for MouseUp.
In the handler, check the args for a right mouse button, return if it's not.
Call treeView.GetNodeAt() with the mouse coordinates to find the node.
Create a context menu.

Here's something similar for a list control which can be adapted for a TreeView:

        private void listJobs_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int index = listJobs.IndexFromPoint(e.Location);
                if (index != ListBox.NoMatches)
                {
                    listJobs.SelectedIndex = index;

                    Job job = (Job)listJobs.Items[index];

                    ContextMenu cm = new ContextMenu();


                    AddMenuItem(cm, "Run", QueueForRun, job).Enabled = !job.Pending;
                    AddMenuItem(cm, "Cancel run", CancelQueueForRun, job).Enabled = (job.State == JobState.Pending || job.State == JobState.Running);
                    AddMenuItem(cm, "Open folder", OpenFolder, job);

                    cm.Show(listJobs, e.Location);
                }
            }
        }

        private MenuItem AddMenuItem(ContextMenu cm, string text, EventHandler handler,     object context)
        {
            MenuItem item = new MenuItem(text, handler);
            item.Tag = context;
            cm.MenuItems.Add(item);
            return item;
        }

You may need to use PointToClient or PointToScreen on the form to translate the coordinates appropriately. You'll soon realize if you need them when the context menu appears in the wrong place.

最单纯的乌龟 2024-08-12 23:58:06

使用 TreeView 上的 ContextMenuStrip 属性添加上下文菜单。如果您不需要显示某些节点​​的菜单,您可以处理 ContextMenuStrip 的 Opening 事件以取消其显示本身 - 或者,您也可以从那里禁用某些菜单选项。

编辑:要抓取鼠标下的节点,处理 TreeView 上的 MouseUp 事件,并使用以下代码:

TreeNode nodeUnderMouse = tvMyTreeView.GetNodeAt(e.X, e.Y);

Use the ContextMenuStrip property on the TreeView to add a context menu. If you need to not show the menu for some of the nodes, you can handle the ContextMenuStrip's Opening event to cancel it from showing itself -- or, you can disable some of the menu's options from there as well.

Edit: to grab the node under the mouse, handle the MouseUp event on the TreeView, and use this code:

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