1.界面
2.后台代码
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 bt_Product_Click(object sender, EventArgs e)
{
int ParentNum =Convert.ToInt32(tb_ParentNum.Text);//获取父节点个数
int ChildNum = Convert.ToInt32(tb_ChildNum.Text);//获取子节点个数
for (int i = 0; i < ParentNum; i++)
{
TreeNode node1 = treeView1.Nodes.Add("父节点" + (i + 1).ToString());
output("添加父节点:" + (i + 1).ToString());
for (int j = 0; j < ChildNum; j++)
{
TreeNode node2 = new TreeNode("子节点" + (j + 1).ToString());
output("添加子节点:" + (j + 1).ToString());
node1.Nodes.Add(node2);
}
}
}
private void output(string log)
{
if (textBox2.GetLineFromCharIndex(textBox2.Text.Length) > 100)
textBox2.Text = "";
textBox2.AppendText(DateTime.Now.ToString("HH:mm:ss ") + log + "\r\n");
Console.Write(log);
}
//清空所有节点
private void bt_Clear_Click(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
output("删除全部节点");
}
//删除选中的节点
private void bt_delete_Click(object sender, EventArgs e)
{
output("删除所选节点:" + treeView1.SelectedNode);
treeView1.Nodes.Remove(treeView1.SelectedNode);
}
//选中事件
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
output("所选节点为:" + e.Node.Text);
}
}
}