还是先上效果图:
主要代码如下:
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()
}
#endregion
private 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
}
}