【问题标题】:TreeNode click vs TreeView clickTreeNode 点击 vs TreeView 点击
【发布时间】:2012-08-24 11:43:01
【问题描述】:

我有一个 TreeView,我需要两件事。

  • 如果我单击特定节点,则支持右键单击。
  • 如果我单击树上的任何其他位置(没有节点),则支持右键单击。

这两个选项都会给我一个不同的ContextMenuStrip
我的两个程序现在支持这两种类型的点击。

特定节点点击:

var someNode = e.Node.Tag as SomeNode;
if (someNode != null)
{
   someContextMenu.Show(someTree, e.Location);
   return;
}

点击树上的任意位置:

问题在于Anywhere on the tree click 事件会在检查我是否点击节点TreeView 的空白处之前触发。
知道如何改变这种行为吗?

【问题讨论】:

    标签: c# winforms treeview nodes contextmenustrip


    【解决方案1】:
    private void treeView1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            TreeViewHitTestInfo info = treeView1.HitTest(e.Location);
            treeView1.SelectedNode = info.Node;
    
            if (info.Node == null)
            {
                contextMenuStrip1.Show(Cursor.Position);
            }
            else
            {
                contextMenuStrip2.Show(Cursor.Position);
            }
        }
    }
    

    或鼠标向上事件,取决于您的需要。您也可以使用 GetNodeAt(e.Location) 代替 TreeViewHitTestInfo 类。

    【讨论】:

      【解决方案2】:

      假设您询问的是 winforms。

      您可以使用TreeView.HitTest 方法返回一个TreeViewHitTestInfo,您可以在其中知道您是否点击了某个节点。

      【讨论】:

      • oups 忘记了标签抱歉。我试试这个
      猜你喜欢
      • 2015-11-07
      • 2014-07-08
      • 2011-12-13
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多