treeview添加二级右键菜
电脑版发表于:2019/10/11 17:20
在一级菜单的基础上使用DropDownItems添加子菜单即可。其实都是一样的代码如下:
public partial class Form1 : Form { public Form1() { InitializeComponent(); treeView1.Nodes.Add("aa"); treeView1.Nodes.Add("bb"); treeView1.Nodes.Add("cc"); //右键菜单 ContextMenuStrip contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(); ToolStripMenuItem menuItem = new ToolStripMenuItem(); menuItem.Text = "子节点"; menuItem.Click += menuItem_Click; ToolStripMenuItem toolStripDropDown = new ToolStripMenuItem(); toolStripDropDown.Text = "一级"; ToolStripMenuItem toolStripDropDown2 = new ToolStripMenuItem(); toolStripDropDown2.Text = "二级"; menuItem.DropDownItems.Add(toolStripDropDown); menuItem.DropDownItems.Add(toolStripDropDown2); contextMenuStrip.Items.Add(menuItem); contextMenuStrip.Items.Add("修改"); contextMenuStrip.Items.Add("添加"); treeView1.ContextMenuStrip = contextMenuStrip; } void menuItem_Click(object sender, EventArgs e) { MessageBox.Show("点击"); } }
效果如下: