Winform中TreeView不同级节点设不同右键菜单

判断TreeNode的Level,根据Level的不同设置不同的右键菜单通常触发的是鼠标点击事件

myTreeView.MouseClick += new MouseEventHandler(myTreeView_MouseClick);

void myTreeView_MouseClick(object sender, MouseEventArgs e)

myTreeView.ContextMenuStrip = null; 

TreeNode selectNode = myTreeView.GetNodeAt(e.X, e.Y); 

if (selectNode.Level == 0)

{

myTreeView.ContextMenuStrip = 你的ContextMenuStrip_1 

else if (selectNode.Level == 1) 

myTreeView.ContextMenuStrip = 你的ContextMenuStrip_2

//继续else if下去,如果你需要... }

你可能感兴趣的:(Winform中TreeView不同级节点设不同右键菜单)