C#界面设计之树目录TreeView的使用

article/2025/9/23 20:25:38

还是先上效果图:
这里写图片描述
主要代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace TreeDemo
{public partial class frmTree : Form{public frmTree(){InitializeComponent();}private List<string> GetAllNodeInfo(TreeView tvDept){List<string> lst = new List<string>();for (int i = 0; i < tvDept.Nodes.Count; i++){string od = string.Empty;od = string.Format("Level:{0},Nodes:{1},Name:{2},Text:{3}\r\n",tvDept.Nodes[i].Level.ToString().PadLeft(3), tvDept.Nodes[i].Nodes.Count.ToString().PadLeft(3),tvDept.Nodes[i].Name, tvDept.Nodes[i].Text);lst.Add(od);if (tvDept.Nodes[i].Nodes.Count > 0){GetAllNodeInfoSub(tvDept.Nodes[i], lst);}}return lst;}private void GetAllNodeInfoSub(TreeNode nodeRoot, List<string> lst){for (int i = 0; i < nodeRoot.Nodes.Count; i++){string od = string.Empty;od = string.Format("Level:{0},Nodes:{1},Name:{2},Text:{3}\r\n",nodeRoot.Nodes[i].Level.ToString().PadLeft(3), nodeRoot.Nodes[i].Nodes.Count.ToString().PadLeft(3),nodeRoot.Nodes[i].Name, nodeRoot.Nodes[i].Text);lst.Add(od);if (nodeRoot.Nodes[i].Nodes.Count > 0){GetAllNodeInfoSub(nodeRoot.Nodes[i], lst);}}}private void frmTree_Load(object sender, EventArgs e){treeView1.ExpandAll();treeView1.HideSelection = false;treeView1.CheckBoxes = checkBox1.Checked;treeView1.ShowLines = checkBox2.Checked;treeView1.ShowRootLines = checkBox3.Checked;}private void treeView1_AfterSelect(object sender, TreeViewEventArgs e){if (e.Node != null){//节点信息string strNodeInfo = string.Empty;strNodeInfo += string.Format("Name:{0}\r\n", e.Node.Name);strNodeInfo += string.Format("Text:{0}\r\n", e.Node.Text);strNodeInfo += string.Format("Nodes:{0}\r\n", e.Node.Nodes.Count.ToString());strNodeInfo += string.Format("Level:{0}\r\n", e.Node.Level);txtNodeInfo.Text = strNodeInfo;//显示窗体switch (e.Node.Text){case "ndForm1":(new Form1()).ShowDialog();break;case "ndForm2":(new Form2()).ShowDialog();break;case "ndForm3":(new Form3()).ShowDialog();break;}}}private void button1_Click(object sender, EventArgs e){TreeNode tn = new TreeNode();tn.Name = tn.Text = "tn" + DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString("D3");treeView1.Nodes.Add(tn);System.Threading.Thread.Sleep(1);}private void button2_Click(object sender, EventArgs e){if (treeView1.SelectedNode == null){return;}else{if (treeView1.SelectedNode.Parent == null){button1_Click(null, null);}else{TreeNode tn = new TreeNode();tn.Name = tn.Text = "tn" + DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString("D3");treeView1.SelectedNode.Parent.Nodes.Add(tn);System.Threading.Thread.Sleep(1);}}}private void button3_Click(object sender, EventArgs e){if (treeView1.SelectedNode == null){return;}else{TreeNode tn = new TreeNode();tn.Name = tn.Text = "tn" + DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString("D3");treeView1.SelectedNode.Nodes.Add(tn);System.Threading.Thread.Sleep(1);treeView1.ExpandAll();}}private void button4_Click(object sender, EventArgs e){if (treeView1.SelectedNode != null){treeView1.Nodes.Remove(treeView1.SelectedNode);}}private void button5_Click(object sender, EventArgs e){if (treeView1.SelectedNode != null){treeView1.SelectedNode.Nodes.Clear();}}private void button7_Click(object sender, EventArgs e){treeView1.ExpandAll();}private void button8_Click(object sender, EventArgs e){treeView1.CollapseAll();}private void checkBox1_CheckedChanged(object sender, EventArgs e){treeView1.CheckBoxes = checkBox1.Checked;treeView1.ExpandAll();}private void checkBox2_CheckedChanged(object sender, EventArgs e){treeView1.ShowLines = checkBox2.Checked;treeView1.ExpandAll();}private void checkBox3_CheckedChanged(object sender, EventArgs e){treeView1.ShowRootLines = checkBox3.Checked;treeView1.ExpandAll();}private void button6_Click(object sender, EventArgs e){List<string> lst = GetAllNodeInfo(treeView1);txtNodeInfo.Text = string.Empty;for (int i = 0; i < lst.Count; i++){txtNodeInfo.Text += lst[i];Application.DoEvents();}}}
}
namespace TreeDemo
{partial class frmTree{/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("节点1");System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("节点4");System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("节点5");System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("节点3", new System.Windows.Forms.TreeNode[] {treeNode2,treeNode3});System.Windows.Forms.TreeNode treeNode5 = new System.Windows.Forms.TreeNode("节点2", new System.Windows.Forms.TreeNode[] {treeNode4});System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("节点0", new System.Windows.Forms.TreeNode[] {treeNode1,treeNode5});System.Windows.Forms.TreeNode treeNode7 = new System.Windows.Forms.TreeNode("节点7");System.Windows.Forms.TreeNode treeNode8 = new System.Windows.Forms.TreeNode("节点8");System.Windows.Forms.TreeNode treeNode9 = new System.Windows.Forms.TreeNode("节点9");System.Windows.Forms.TreeNode treeNode10 = new System.Windows.Forms.TreeNode("节点10");System.Windows.Forms.TreeNode treeNode11 = new System.Windows.Forms.TreeNode("节点6", new System.Windows.Forms.TreeNode[] {treeNode7,treeNode8,treeNode9,treeNode10});System.Windows.Forms.TreeNode treeNode12 = new System.Windows.Forms.TreeNode("ndForm1");System.Windows.Forms.TreeNode treeNode13 = new System.Windows.Forms.TreeNode("ndForm2");System.Windows.Forms.TreeNode treeNode14 = new System.Windows.Forms.TreeNode("ndForm3");System.Windows.Forms.TreeNode treeNode15 = new System.Windows.Forms.TreeNode("导航", new System.Windows.Forms.TreeNode[] {treeNode12,treeNode13,treeNode14});this.treeView1 = new System.Windows.Forms.TreeView();this.lblInfo = new System.Windows.Forms.Label();this.txtNodeInfo = new System.Windows.Forms.TextBox();this.button1 = new System.Windows.Forms.Button();this.button2 = new System.Windows.Forms.Button();this.button3 = new System.Windows.Forms.Button();this.button4 = new System.Windows.Forms.Button();this.button5 = new System.Windows.Forms.Button();this.button7 = new System.Windows.Forms.Button();this.button8 = new System.Windows.Forms.Button();this.checkBox1 = new System.Windows.Forms.CheckBox();this.checkBox2 = new System.Windows.Forms.CheckBox();this.checkBox3 = new System.Windows.Forms.CheckBox();this.button6 = new System.Windows.Forms.Button();this.SuspendLayout();// // treeView1// this.treeView1.BackColor = System.Drawing.SystemColors.Window;this.treeView1.Location = new System.Drawing.Point(12, 12);this.treeView1.Name = "treeView1";treeNode1.Name = "节点1";treeNode1.Text = "节点1";treeNode2.Name = "节点4";treeNode2.Text = "节点4";treeNode3.Name = "节点5";treeNode3.Text = "节点5";treeNode4.Name = "节点3";treeNode4.Text = "节点3";treeNode5.Name = "节点2";treeNode5.Text = "节点2";treeNode6.Name = "节点0";treeNode6.Text = "节点0";treeNode7.Name = "节点7";treeNode7.Text = "节点7";treeNode8.Name = "节点8";treeNode8.Text = "节点8";treeNode9.Name = "节点9";treeNode9.Text = "节点9";treeNode10.Name = "节点10";treeNode10.Text = "节点10";treeNode11.Name = "节点6";treeNode11.Text = "节点6";treeNode12.Name = "ndForm1";treeNode12.Text = "ndForm1";treeNode13.Name = "ndForm2";treeNode13.Text = "ndForm2";treeNode14.Name = "ndForm3";treeNode14.Text = "ndForm3";treeNode15.Name = "节点11";treeNode15.Text = "导航";this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {treeNode6,treeNode11,treeNode15});this.treeView1.Size = new System.Drawing.Size(288, 449);this.treeView1.TabIndex = 0;this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);// // lblInfo// this.lblInfo.Location = new System.Drawing.Point(306, 12);this.lblInfo.Name = "lblInfo";this.lblInfo.Size = new System.Drawing.Size(474, 21);this.lblInfo.TabIndex = 2;this.lblInfo.Text = "选中的节点信息";// // txtNodeInfo// this.txtNodeInfo.Location = new System.Drawing.Point(306, 36);this.txtNodeInfo.Multiline = true;this.txtNodeInfo.Name = "txtNodeInfo";this.txtNodeInfo.ReadOnly = true;this.txtNodeInfo.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;this.txtNodeInfo.Size = new System.Drawing.Size(474, 226);this.txtNodeInfo.TabIndex = 3;// // button1// this.button1.Location = new System.Drawing.Point(306, 268);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(75, 23);this.button1.TabIndex = 4;this.button1.Text = "添加根";this.button1.UseVisualStyleBackColor = true;this.button1.Click += new System.EventHandler(this.button1_Click);// // button2// this.button2.Location = new System.Drawing.Point(387, 268);this.button2.Name = "button2";this.button2.Size = new System.Drawing.Size(75, 23);this.button2.TabIndex = 5;this.button2.Text = "添加同级";this.button2.UseVisualStyleBackColor = true;this.button2.Click += new System.EventHandler(this.button2_Click);// // button3// this.button3.Location = new System.Drawing.Point(468, 268);this.button3.Name = "button3";this.button3.Size = new System.Drawing.Size(75, 23);this.button3.TabIndex = 6;this.button3.Text = "添加子级";this.button3.UseVisualStyleBackColor = true;this.button3.Click += new System.EventHandler(this.button3_Click);// // button4// this.button4.Location = new System.Drawing.Point(549, 268);this.button4.Name = "button4";this.button4.Size = new System.Drawing.Size(75, 23);this.button4.TabIndex = 7;this.button4.Text = "删除选中";this.button4.UseVisualStyleBackColor = true;this.button4.Click += new System.EventHandler(this.button4_Click);// // button5// this.button5.Location = new System.Drawing.Point(630, 268);this.button5.Name = "button5";this.button5.Size = new System.Drawing.Size(150, 23);this.button5.TabIndex = 8;this.button5.Text = "删除选中子级";this.button5.UseVisualStyleBackColor = true;this.button5.Click += new System.EventHandler(this.button5_Click);// // button7// this.button7.Location = new System.Drawing.Point(306, 297);this.button7.Name = "button7";this.button7.Size = new System.Drawing.Size(75, 23);this.button7.TabIndex = 4;this.button7.Text = "展开";this.button7.UseVisualStyleBackColor = true;this.button7.Click += new System.EventHandler(this.button7_Click);// // button8// this.button8.Location = new System.Drawing.Point(387, 297);this.button8.Name = "button8";this.button8.Size = new System.Drawing.Size(75, 23);this.button8.TabIndex = 5;this.button8.Text = "关闭";this.button8.UseVisualStyleBackColor = true;this.button8.Click += new System.EventHandler(this.button8_Click);// // checkBox1// this.checkBox1.AutoSize = true;this.checkBox1.Location = new System.Drawing.Point(306, 336);this.checkBox1.Name = "checkBox1";this.checkBox1.Size = new System.Drawing.Size(78, 16);this.checkBox1.TabIndex = 9;this.checkBox1.Text = "CheckBoxs";this.checkBox1.UseVisualStyleBackColor = true;this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);// // checkBox2// this.checkBox2.AutoSize = true;this.checkBox2.Location = new System.Drawing.Point(306, 358);this.checkBox2.Name = "checkBox2";this.checkBox2.Size = new System.Drawing.Size(78, 16);this.checkBox2.TabIndex = 10;this.checkBox2.Text = "ShowLines";this.checkBox2.UseVisualStyleBackColor = true;this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged);// // checkBox3// this.checkBox3.AutoSize = true;this.checkBox3.Location = new System.Drawing.Point(306, 380);this.checkBox3.Name = "checkBox3";this.checkBox3.Size = new System.Drawing.Size(78, 16);this.checkBox3.TabIndex = 11;this.checkBox3.Text = "ShowLines";this.checkBox3.UseVisualStyleBackColor = true;this.checkBox3.CheckedChanged += new System.EventHandler(this.checkBox3_CheckedChanged);// // button6// this.button6.Location = new System.Drawing.Point(468, 297);this.button6.Name = "button6";this.button6.Size = new System.Drawing.Size(75, 23);this.button6.TabIndex = 12;this.button6.Text = "遍历";this.button6.UseVisualStyleBackColor = true;this.button6.Click += new System.EventHandler(this.button6_Click);// // frmTree// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(784, 462);this.Controls.Add(this.button6);this.Controls.Add(this.checkBox3);this.Controls.Add(this.checkBox2);this.Controls.Add(this.checkBox1);this.Controls.Add(this.button5);this.Controls.Add(this.button4);this.Controls.Add(this.button3);this.Controls.Add(this.button8);this.Controls.Add(this.button2);this.Controls.Add(this.button7);this.Controls.Add(this.button1);this.Controls.Add(this.txtNodeInfo);this.Controls.Add(this.lblInfo);this.Controls.Add(this.treeView1);this.MaximizeBox = false;this.MaximumSize = new System.Drawing.Size(800, 500);this.MinimizeBox = false;this.MinimumSize = new System.Drawing.Size(800, 500);this.Name = "frmTree";this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;this.Text = "frmTree.cs";this.Load += new System.EventHandler(this.frmTree_Load);this.ResumeLayout(false);this.PerformLayout();}#endregionprivate System.Windows.Forms.TreeView treeView1;private System.Windows.Forms.Label lblInfo;private System.Windows.Forms.TextBox txtNodeInfo;private System.Windows.Forms.Button button1;private System.Windows.Forms.Button button2;private System.Windows.Forms.Button button3;private System.Windows.Forms.Button button4;private System.Windows.Forms.Button button5;private System.Windows.Forms.Button button7;private System.Windows.Forms.Button button8;private System.Windows.Forms.CheckBox checkBox1;private System.Windows.Forms.CheckBox checkBox2;private System.Windows.Forms.CheckBox checkBox3;private System.Windows.Forms.Button button6;}
}

http://chatgpt.dhexx.cn/article/WSlqJc2W.shtml

相关文章

treeview 跨窗体拖拽功能的实现(一)

功能实现&#xff1a; 1.从工具窗体往任务窗体拖拽&#xff1b; 2.在任务窗体中改变节点的顺序&#xff1b; 3.右键复制节点&#xff0c;删除节点 工具窗体treeview1,&#xff1a; 涉及使用的事件&#xff1a;DragEnter&#xff0c; DragOver ,ItemDrag, NodeMouseDouble…

Virtual Treeview 安装以及入门

Virtual Treeview是一套Delphi下优秀的VCL控件&#xff0c;代码质量高&#xff0c;使用灵活、功能强大、性能非常好&#xff0c;可以用于表达Treeview和表格类数据。它的代码现在托管在google code上。 Virtual Treeview是一个“纯VCL”控件&#xff0c;这意味着它不是基于任何…

android treeview 树形结构,前端开发中,使用TreeView控件创建树形结构

原标题&#xff1a;前端开发中&#xff0c;使用TreeView控件创建树形结构 Wijmo是一款使用Type编写的新一代Java/HTML5控件集。它秉承触控优先的设计理念&#xff0c;在全球率先支持AngularJS&#xff0c;并提供性能卓越、零依赖的FlexGrid和图表等多个控件。 我们已经知道在Wi…

透透彻彻了解服务器技术

什么是服务器   服务器是一种高性能计算机&#xff0c;作为网络的节点&#xff0c;存储、处理网络上80&#xff05;的数据、信息&#xff0c;因此也被称为网络的灵魂。做一个形象的比喻&#xff1a;服务器就像是邮局的交换机&#xff0c;而微机、笔记本、PDA、手机等固定或移…

服务器的介绍

一 、IDC/机柜/物理server/云主机介绍 1.互联网数据中心 &#xff08;Internet Data Center&#xff09; 互联网数据中心图片 &#xff08;1&#xff09;IDC的简介 简称IDC&#xff0c;是电信部门利用已有的互联网通信线路、带宽资源&#xff0c;建立的标准化电信专业级机房环…

web服务器(技术讲解)

2.Web服务器&#xff08;技术讲解&#xff09; 1.ASP 微软&#xff1a;国内最早流行在HTML中嵌入了VB脚本&#xff0c;ASPCOM在ASP开发中&#xff0c;基本一个页面有几千行的业务代码&#xff0c;页面杂乱&#xff0c;维护成本非常高 2.PHP 开发速度很快&#xff0c;功能强…

关于服务器

初始服务器 云操作 以下是重装系统操作 出现黑窗口后&#xff1a; 输入—>sudo passwd命令–>输入密码&#xff08;密码不可见&#xff09;–>再次输入密码&#xff08;密码不可见&#xff09;–>su命令&#xff08;可使用root用户&#xff09;–>输入密码&…

云服务器简介

云服务器简介 一、云服务器二、云服务的灵魂——虚拟化三、云服务器ECS概念 一、云服务器 1、云服务器简介 云计算服务器又称为云服务器或云主机&#xff0c;是云计算服务体系中的一项主机产品&#xff0c;它有效地解决了传统物理主机与VPS服务中&#xff0c;存在的管理难度打…

服务器概述

1、什么是服务器&#xff1f; 服务器&#xff1a;分为服务器硬件和服务器软件。在硬件服务器&#xff08;计算机&#xff09;上安装服务器软件&#xff0c;才可以对外提供服务。 比如&#xff1a;让其他的计算机访问当前服务器&#xff0c;为其他的计算机提供服务。 &#xff…

服务器技术(三)--Nginx

Nginx介绍 Nginx是什么、适用场景 Nginx是一个高性能的HTTP和反向代理服务器&#xff0c;特点是占有内存少&#xff0c;并发能力强&#xff0c;事实上nginx的并发能力确实在同类型的网页服务器中表现较好。 Nginx专为性能优化而开发&#xff0c;性能是其最重要的考量&#xf…

2022年,服务器领域十大技术趋势

2020年&#xff0c;疫情的全球爆发对上半年服务器市场的出货量带来了不小的影响。但是随着下半年疫情逐渐得到控制以及数字化需求的激增&#xff0c;全球服务器市场呈现出非常不错的增长态势。 目前来看&#xff0c;服务器依然是数字化转型和云计算、互联网等技术发展基础&…

服务器端技术

简介&#xff1a; 服务器分为web服务器和应用服务器。Web服务器是离客户端最近的服务器&#xff0c;负责监听和处理HTTP请求。应用服务器比web服务器更靠近后端&#xff0c;主要处理复杂的业务逻辑和数据库的访问。 如果是静态资源&#xff08;例如HTML页面或图片&#xff09;…

云计算服务器技术概述

云计算服务器技术概述 概述 云主机也叫云服务器ECS (Elastic Compute Server)&#xff0c;是一种按需获取的云端服务器&#xff0c;提供高可靠、弹性扩展的计算资源服务&#xff0c;可以根据需求选择不同规格的CPU、内存、操作系统、硬盘和网络来创建云主机&#xff0c;满足个性…

什么是服务器 ?服务器常见的系统和技术有哪些?

就像他的名字一样&#xff0c;服务器在网络上为不同用户提供不同内容的信息、资料和文件。可以说服务器就是Internet网络上的资源仓库&#xff0c;正是因为有着种类繁多数量庞大内容丰富的服务器的存在&#xff0c;才使得Internet如此的绚丽多彩。 服务器的种类和功能: (1) WW…

“东数西算”下数据中心的液冷GPU服务器如何发展?

以云计算、大数据、人工智能、高性能计算、数据分析、数据挖掘等为首的新兴技术凭借其优越的便利性和稳定性开始为服务器市场带来新的生机活力&#xff0c;其中以GPU为首的AI服务器在一定程度上不仅重构现行的产业结构&#xff0c;也为整个市场带来了新的有效增量。那么服务器的…

服务器技术基础

服务器技术基础 服务器是计算机的一种&#xff0c;比普通计算机运行更快、负载更高、价格更贵。服务器在网络中为其它客户机&#xff08;如PC机、智能手机、ATM等终端甚至是火车系统等大型设备&#xff09;提供计算或者应用服务。服务器具有高速的CPU运算能力、长时间的可靠运行…

1、NetBIOS简介

“网络基本输入&#xff0f;输出系统” &#xff08;Network Basic Input/Output System, NetBIOS&#xff09;是一种标准的应用程序编程接口&#xff08;A P I&#xff09;&#xff0c;1983年由Sytek公司专为IBM开发成功。NetBIOS为网络通信定义了一种编程接口&#xff0c;但却…

NetBIOS初步了解

NetBIOS定义及作用 NetBIOS&#xff1a;是Network Basic Input/Output System的简称&#xff0c;一般指用于局域网通信的一套API。作用是为了给局域网提供网络以及其他特殊功能&#xff0c;系统可以利用WINS服务、广播及Lmhost文件等多种模式将主机名解析为相应IP地址&#xf…

BIOS 常用网站 (持续更新中)

UEFI 官网 下载UEFI/PI/ACPI 等SPEC Welcome to Unified Extensible Firmware Interface Forum | Unified Extensible Firmware Interface Forumhttps://uefi.org/ AMD资料 (需账号权限) 下载AMD芯片资料 Login – AMD DevHubhttps://devhub.amd.com/ Rw工具 OS下读取寄存…

更改netbios计算机名,NetBIOS主机名解析

原创]NetBIOS主机名解析 NetBIOS主机名解析主要使用其子协议NetBIOS Name Service协议来进行。 工作组&#xff1a;是网络中具有相同“Workgroup”名字的主机组成。用户可以修改此组名子加入另一个组。 属于同一个工作组的主机才能相互通告主机名&#xff0c;不同的工作组之间不…