WinForms中动态给treeView的节点添加ContextMenuStrip,并绑定Click事件

生成ContextMenuStrip

       var docMenu = new ContextMenuStrip();
           ToolStripMenuItem deleteMenuItem = new ToolStripMenuItem();
             deleteMenuItem.Text = "Delete"; 
             deleteMenuItem.Click += new EventHandler(deleteMenuItem_Click); //给添加的ContextMenuStrip绑定click事件
        docMenu.Items.AddRange(new ToolStripMenuItem[] { deleteMenuItem });

将docMenu添加到treeView中去

   DataTreeView.Nodes[0].ContextMenuStrip = docMenu;

添加绑定事件(函数名称要和上面绑定时候的名称一样)

 private void deleteMenuItem_Click(object sender, EventArgs e)
        {
           MessageBox.show("click delete");
        }

 

posted @ 2019-08-16 16:41 an_blog 阅读(...) 评论(...) 编辑 收藏