新建一个窗体,在本窗体界面上需要以下几个按钮
(一个TreeView 一个 TextBox 三个Button 按钮)
后台代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { treeView1.Nodes.Clear(); TreeNode tree = new TreeNode("根节点"); treeView1.Nodes.Add(tree); } /// <summary> /// 添加子节点的方法 /// </summary> public void addChildCode() { //首先判断是否选中节点中的位置 if (treeView1.SelectedNode == null) { MessageBox.Show("请选择一个节点!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (textBox1.Text != "") { //创建一个节点对象并初始化 TreeNode tmp = new TreeNode(textBox1.Text); //在TreeView组件中加入子节点 treeView1.SelectedNode.Nodes.Add(tmp); treeView1.SelectedNode = tmp; treeView1.ExpandAll(); } else { MessageBox.Show("textbox组件必须填写节点名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } } /// <summary> /// 添加兄弟节点的方法 /// </summary> public void addParent() { try { if (treeView1.SelectedNode == null) { MessageBox.Show("请选择一个节点!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (textBox1.Text != null) { //创建一个节点对象并且初始化 TreeNode tmp = new TreeNode(textBox1.Text); //在TreeView 组件中加入兄弟节点 treeView1.SelectedNode.Parent.Nodes.Add(tmp); treeView1.ExpandAll(); } else { MessageBox.Show("textbox组件必须填写节点名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } } catch { TreeNode rmp = new TreeNode("根节点"); treeView1.Nodes.Add(rmp); } } /// <summary> /// 判断鼠标点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void treeView1_MouseDown(object sender, MouseEventArgs e) { //获取是否是鼠标右键点击 if (e.Button == MouseButtons.Right) { contextMenuStrip1.Show(this, new Point(e.X, e.Y)); } } /// <summary> /// 展开下一节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { treeView1.SelectedNode.Expand(); } /// <summary> /// 展开全部节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { //定位根节点 treeView1.SelectedNode = treeView1.Nodes[]; //展开组件中的所有节点 treeView1.SelectedNode.ExpandAll(); } /// <summary> /// 折叠全部节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { //定位根节点 treeView1.SelectedNode = treeView1.Nodes[]; //收缩组件中的所有节点 treeView1.SelectedNode.Collapse(); } /// <summary> /// 点击快捷菜单中的“加入子节点”菜单项,则调用用户自定义的addChildCode()方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void 加入子节点ToolStripMenuItem_Click_1(object sender, EventArgs e) { addChildCode(); } /// <summary> /// 点击快捷菜单中的“加入兄弟节点”菜单项,则调用用户自定义的addParent()方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void 加入兄弟节点ToolStripMenuItem_Click_1(object sender, EventArgs e) { addParent(); } /// <summary> /// 点击快捷菜单中的“删除节点”菜单项,则删除节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void 删除节点ToolStripMenuItem_Click(object sender, EventArgs e) { ) { treeView1.SelectedNode.Remove(); } else { MessageBox.Show("请先删除此节点中的子节点!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }