如何仅当用户单击 TreeView 中的根节点时才显示上下文菜单?

发布于 2024-11-02 01:35:29 字数 393 浏览 0 评论 0 原文

我有一个 TreeView 和一个上下文菜单。我只想在右键单击根节点而不是子节点时显示上下文菜单。

这是我到目前为止所拥有的。即使我右键单击子节点,也会显示上下文菜单。如何更改此设置,以便仅在右键单击根节点时才显示菜单?是否可以?

if(e.Button == MouseButtons.Right)
{
    // Select the clicked node
    treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);

    if(treeView1.SelectedNode != null)
    {
        myContextMenuStrip.Show(treeView1, e.Location)
    }
}

I have a TreeView and a Context Menu. I want to show the Context Menu ONLY when I right click on the ROOT node and not the child nodes.

This is what I have so far. This shows the Context Menu even when I right click on the child nodes. How can I change this so that the Menu shows only when I right click on the root node? Is it possible?

if(e.Button == MouseButtons.Right)
{
    // Select the clicked node
    treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);

    if(treeView1.SelectedNode != null)
    {
        myContextMenuStrip.Show(treeView1, e.Location)
    }
}

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

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

发布评论

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

评论(3

做个ˇ局外人 2024-11-09 01:35:29

是的,这是可能的,但您需要在 if 语句中添加一些逻辑,以验证用户单击的节点是否为根节点。

但是我们如何知道它是否是根节点呢?好吧,仔细想想,我们可以将根节点定义为没有任何父节点的节点。因此,您只需检查 Parent< TreeNode 的 /code> 属性 并确保其为 null

将您的代码修改为如下所示:

if (e.Button == MouseButtons.Right)
{
    // Select the clicked node
    treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);

    if (treeView1.SelectedNode != null && treeView.SelectedNode.Parent == null)
    {
        myContextMenuStrip.Show(treeView1, e.Location)
    }
}

您希望保留节点本身不为 null 的检查,因为您不希望在它们还没有<时显示上下文菜单/em> 单击了一个节点,但您需要添加对父节点的检查,因为这会告诉您它是否是根节点。以编程方式指示的方式是使用逻辑 AND,即 C# 中的 && 运算符。

Yes, it's possible, but you'll need to add some logic to your if statements that verifies the node the user clicked on is a root node.

But how do we find out if it's a root node? Well, thinking it through, we can define a root node as one that does not have any parents. So you can simply check the Parent property of the TreeNode and make sure that it is null.

Modify your code to look something like this:

if (e.Button == MouseButtons.Right)
{
    // Select the clicked node
    treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);

    if (treeView1.SelectedNode != null && treeView.SelectedNode.Parent == null)
    {
        myContextMenuStrip.Show(treeView1, e.Location)
    }
}

You want to retain the check that the node itself is not null, because you don't want to show the context menu when they haven't clicked on a node, but you need to add the check for a parent, because that tells you whether or not it's a root node. The way you indicate that programmatically is using a logical AND, which is the && operator in C#.

我最亲爱的 2024-11-09 01:35:29

检查您单击的节点是否为根节点,而不是检查它是否为 null

Check that the node you clicked on is the root node instead of checking for it being null.

高速公鹿 2024-11-09 01:35:29

您还可以使用 Level 属性:

http://msdn.microsoft.com/EN-US/library/386b25wy(v=VS.110,d=hv.2).aspx

If e.Button = MouseButtons.Right Then
  ' Select the clicked node
  treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y)

  If treeView1.SelectedNode.Level = 0 Then
    myContextMenuStrip.Show(treeView1, e.Location)
  End If
End If

You can also use the Level property:

http://msdn.microsoft.com/EN-US/library/386b25wy(v=VS.110,d=hv.2).aspx

If e.Button = MouseButtons.Right Then
  ' Select the clicked node
  treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y)

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