TreeView的用法总结
1、循环往treeview中添加数据
public AuthorNavUserControl() { InitializeComponent(); LoadTrees(MainForm.ProjectId,treeView1.Nodes); } private void LoadTrees(Guid guid, TreeNodeCollection treeNodeCollection) { List<IetmProject> ietmProjectlist = new List<IetmProject>(); IIetmProjectService itemProjectService = new IetmProjectService(); ietmProjectlist = itemProjectService.GetIetmProjectId(guid); if (ietmProjectlist != null) { foreach (IetmProject item in ietmProjectlist) { TreeNode node = treeNodeCollection.Add(item.MICName); node.Tag = item.ObjectId; LoadTrees((Guid)item.ObjectId, node.Nodes); } } }
2、右击节点弹出“显示”菜单,点击“显示”后弹出该节点的id值
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 is TreeNode)//判断点的是不是一个节点 { treeView1.SelectedNode = CurrentNode; CurrentNode.ContextMenuStrip = this.contextMenuStrip1; contextMenuStrip1.Show(MousePosition); } } } private void 显示ToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show(treeView1.SelectedNode.Tag.ToString()); }