转自http://www.cnblogs.com/luluping/archive/2010/03/03/1677206.html
(1)要向窗体添加一个TreeView控件
(2)再添加一个ContextMenuStrip控件;
(3)就要给TreeView添加一个MouseDown事件,代码如下:
private void treeView1_MouseDown(object sender, MouseEventArgs e) ...{ if (e.Button == MouseButtons.Right)//判断你点的是不是右键 ...{ Point ClickPoint = new Point(e.X, e.Y); TreeNode CurrentNode = treeView1.GetNodeAt(ClickPoint); if (CurrentNode != null)//判断你点的是不是一个节点 ...{ switch (CurrentNode.Name)//根据不同节点显示不同的右键菜单,当然你可以让它显示一样的菜单 ...{ case "errorUrl": CurrentNode.ContextMenuStrip = contextMenuStripErrorUrl; break; } treeView1.SelectedNode = CurrentNode;//选中这个节点 } } }
时间: 2024-10-05 17:09:26