TreeView添加快键菜单有两种方法:

一种就是使用TreeView的ContextMenuStrip属性,添加一个新ContextMenuStrip,这个方法非常的简答直接,缺点是右键菜单是整个控件响应的,也就是说即使没有右键选中节点也是会触发快捷菜单的显示

这种方法里获取哪一个的node选中是通过这个方法:TreeNode curNode = this.trvFolder.GetNodeAt(e.X, e.Y)

另一种是创建ContextMenuStrip,并且使用TreeView的NodeMouseClick事件,在事件中实现为:

private void trvFolder_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Point pos = new Point(e.Node.Bounds.X + e.Node.Bounds.Width, e.Node.Bounds.Y + e.Node.Bounds.Height / 2);
                this.cmsFolderMenu.Show(this.trvFolder, pos);
            }
}

相关文章:

  • 2022-01-15
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2022-12-23
  • 2021-10-27
  • 2022-12-23
  • 2021-11-18
猜你喜欢
  • 2021-08-28
  • 2021-11-25
  • 2021-05-10
  • 2022-12-23
  • 2022-12-23
  • 2021-07-28
  • 2022-01-06
相关资源
相似解决方案