一、方案一:免递归使用树
cs
复制代码
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 自定义树实体
/// </summary>
public class WinFormTree
{
/// <summary>
/// 标签名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 本节点ID(根节点Pid=0)
/// </summary>
public int Id { get; set; }
/// <summary>
/// 父节点ID
/// </summary>
public int Pid { get; set; }
}
//点击触发自定义弹窗输入
private async void button1_Click(object sender, EventArgs e)
{
// 假设这是数据源
List<WinFormTree> data = new List<WinFormTree>
{
new WinFormTree { Id = 1, Name = "爷爷", Pid = 0 },
new WinFormTree { Id = 2, Name = "父亲", Pid = 1 },
new WinFormTree { Id = 3, Name = "儿子", Pid = 2 },
new WinFormTree { Id = 4, Name = "女儿", Pid = 2 } // 假设加个子节点
};
// 调用方法来构建树
BuildTree(treeView1,data);
}
/// <summary>
/// 构建树
/// </summary>
/// <param name="data"></param>
private void BuildTree(TreeView treeView,List<WinFormTree> data)
{
//清空树
treeView.Nodes.Clear();
// 使用字典存储 id -> TreeNode 的映射
Dictionary<int, TreeNode> nodeDict = new Dictionary<int, TreeNode>();
// 根节点
TreeNode rootNode = null;
// 遍历所有数据,构建树
foreach (var item in data)
{
TreeNode node = new TreeNode(item.Name);
// 存储当前节点
nodeDict.Add(item.Id, node);
// 如果是根节点,保存根节点
if (item.Pid == 0)
{
rootNode = node;
}
else
{
// 找到父节点并将当前节点添加到父节点下
if (nodeDict.ContainsKey(item.Pid))
{
nodeDict[item.Pid].Nodes.Add(node);
}
}
}
// 将树根节点添加到 TreeView 中
if (rootNode != null)
{
treeView.Nodes.Add(rootNode);
}
}
/// <summary>
/// 树节点选中事件(双击进入该方法)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
// 获取选中的节点的文本内容
string selectedNodeText = e.Node.Text;
// 将选中的节点文本设置到Label控件
label1.Text = "选中的标签是: " + selectedNodeText;
}
}
}
二、方案二:递归求树
cs
复制代码
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 自定义树实体
/// </summary>
public class WinFormTree
{
/// <summary>
/// 标签名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 本节点ID(根节点Pid=0)
/// </summary>
public int Id { get; set; }
/// <summary>
/// 父节点ID
/// </summary>
public int Pid { get; set; }
}
//点击触发自定义弹窗输入
private async void button1_Click(object sender, EventArgs e)
{
// 假设这是数据源
List<WinFormTree> data = new List<WinFormTree>
{
new WinFormTree { Id = 1, Name = "爷爷", Pid = 0 },
new WinFormTree { Id = 2, Name = "父亲", Pid = 1 },
new WinFormTree { Id = 3, Name = "儿子", Pid = 2 },
new WinFormTree { Id = 4, Name = "女儿", Pid = 2 } // 假设加个子节点
};
// 调用方法来构建树
BuildTree(treeView1,data);
}
/// <summary>
/// 构建树
/// </summary>
/// <param name="data"></param>
private void BuildTree(TreeView treeView,List<WinFormTree> data)
{
//清空树
treeView.Nodes.Clear();
// 根节点
TreeNode rootNode = GetTreeByData(data);
// 将树根节点添加到 TreeView 中
if (rootNode != null)
{
treeView.Nodes.Add(rootNode.FirstNode);
}
}
private TreeNode GetTreeByData(List<WinFormTree> data, int parentId = 0)
{
// 使用 LINQ 获取所有子节点
var nodes = data
.Where(x => x.Pid == parentId)
.Select(item =>
{
var node = new TreeNode(item.Name);
node.Nodes.AddRange(GetTreeByData(data, item.Id).Nodes.Cast<TreeNode>().ToArray()); // 递归添加子节点
return node;
})
.ToList();
// 创建一个父节点并将子节点添加进去
var parentNode = new TreeNode();
parentNode.Nodes.AddRange(nodes.ToArray()); // 添加所有子节点
return parentNode;
}
/// <summary>
/// 树节点选中事件(双击进入该方法)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
// 获取选中的节点的文本内容
string selectedNodeText = e.Node.Text;
// 将选中的节点文本设置到Label控件
label1.Text = "选中的标签是: " + selectedNodeText;
}
}
}
三、效果图