欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# TreeView添加右键快键菜单有两种方法

程序员文章站 2023-12-17 14:50:46
一种就是使用treeview的contextmenustrip属性,添加一个新contextmenustrip,这个方法非常的简答直接,缺点是右键菜单是整个控件响应的,也就...

一种就是使用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);
            }
}

上一篇:

下一篇: