本篇存放在学习过程中遇到的各种C#控件使用方法
TreeView控件使用:
private void TreeViewForm_Load(object sender, EventArgs e) { treeView.LabelEdit = true; //可编辑状态 TreeNode node = new TreeNode(); //新建一个根节点 node.Text = "根节点1"; treeView.Nodes.Add(node); //添加根节点 TreeNode node1 = new TreeNode(); node1.Text = "节点1"; TreeNode node11 = new TreeNode(); node11.Text = "节点11"; TreeNode node2 = new TreeNode(); node2.Text = "节点2"; node1.Nodes.Add(node11); //在node1下添加一个节点node11 node.Nodes.Add(node1); //在根节点下添加节点node1、node2 node.Nodes.Add(node2); TreeNode t = new TreeNode("根节点2"); node.Text = "根节点1"; treeView.Nodes.Add(t); //添加根节点 TreeNode t1 = new TreeNode(); t1.Text = "节点1"; TreeNode t11 = new TreeNode(); t11.Text = "节点11"; TreeNode t2 = new TreeNode(); t2.Text = "节点2"; t1.Nodes.Add(t11); //在t1下添加一个节点t11 t.Nodes.Add(t1); //在根节点下添加节点t1、t2 t.Nodes.Add(t2); } private void treeView_AfterSelect(object sender, TreeViewEventArgs e) { label_SelectResult.Text = treeView.SelectedNode.Text; //获取选中节点Text } private void button_Edit_Click(object sender, EventArgs e) { treeView.SelectedNode.BeginEdit(); //编辑选中的节点 } private void button_Delete_Click(object sender, EventArgs e) { treeView.Nodes.Remove(treeView.SelectedNode); //删除选中节点 }