树形框treeView演示程序
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Tree
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
p rivate System.Windows.Forms.TreeView treeView1;
p rivate System.Windows.Forms.Label label1;
p rivate System.Windows.Forms.TextBox textBox1;
p rivate System.Windows.Forms.Button button1;
p rivate System.Windows.Forms.Button button2;
p rivate System.Windows.Forms.Button button3;
p rivate System.Windows.Forms.Button button4;
p rivate System.Windows.Forms.ImageList imageList1;
p rivate System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
p rivate void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.treeView1 = new System.Windows.Forms.TreeView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = 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.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageList = this.imageList1;
this.treeView1.Location = new System.Drawing.Point(8, 8);
this.treeView1.Name = "treeView1";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("中华人民共和国", new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("北京市"),
new System.Windows.Forms.TreeNode("上海市")})});
this.treeView1.Size = new System.Drawing.Size(208, 148);
this.treeView1.TabIndex = 0;
this.treeView1.AfterExpand += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterExpand);
this.treeView1.AfterCollapse += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterCollapse);
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// label1
//
this.label1.Location = new System.Drawing.Point(224, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 16);
this.label1.TabIndex = 1;
this.label1.Text = "树节点标签内容: ";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(224, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "罗斌";
//
// button1
//
this.button1.Location = new System.Drawing.Point(224, 56);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 23);
this.button1.TabIndex = 3;
this.button1.Text = "添加为根节点";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(224, 80);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(128, 23);
this.button2.TabIndex = 4;
this.button2.Text = "添加为子节点";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(224, 104);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(128, 23);
this.button3.TabIndex = 5;
this.button3.Text = "删除选中节点";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(224, 128);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(128, 23);
this.button4.TabIndex = 6;
this.button4.Text = "关闭应用程序";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(360, 166);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.treeView1);
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "演示树视图控件";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
p rivate void treeView1_AfterExpand(object sender, System.Windows.Forms.TreeViewEventArgs e)
{//处理节点展开事件
e.Node.ImageIndex=1;
e.Node.SelectedImageIndex=1;
}
p rivate void treeView1_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e)
{//处理节点收缩事件
e.Node.ImageIndex=0;
e.Node.SelectedImageIndex=0;
}
p rivate void button1_Click(object sender, System.EventArgs e)
{//添加根节点
TreeNode newNode=new TreeNode(this.textBox1.Text,0,0);
this.treeView1.Nodes.Add(newNode);
this.treeView1.Select();
}
p rivate void button2_Click(object sender, System.EventArgs e)
{//添加子节点
TreeNode selectedNode=this.treeView1.SelectedNode;
if(selectedNode==null)
{
MessageBox.Show(this,"添加子节点之前必须先选中一个根节点。","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
TreeNode newNode=new TreeNode(this.textBox1.Text,1,1);
selectedNode.Nodes.Add(newNode);
selectedNode.SelectedImageIndex=1;
selectedNode.Expand();
this.treeView1.Select();
}
p rivate void button3_Click(object sender, System.EventArgs e)
{//删除选定的节点
TreeNode selectedNode=this.treeView1.SelectedNode;
if(selectedNode==null)
{
MessageBox.Show(this,"删除节点之前必须先选中一个节点。","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
TreeNode parentNode=selectedNode.Parent;
if(parentNode==null)
{
this.treeView1.Nodes.Remove(selectedNode);
}
else
{
parentNode.Nodes.Remove(selectedNode);
if(parentNode.Nodes.Count==0)
{
parentNode.ImageIndex=1;
parentNode.SelectedImageIndex=1;
}
}
this.treeView1.Select();
}
p rivate void Form1_Load(object sender, System.EventArgs e)
{//初始化示例节点
TreeNode RootNode=new TreeNode("中华人民共和国",0,0);
this.treeView1.Nodes.Add(RootNode);
this.treeView1.Select();
TreeNode SubNode1=new TreeNode("北京市",1,1);
RootNode.Nodes.Add(SubNode1);
TreeNode SubNode2=new TreeNode("上海市",1,1);
RootNode.Nodes.Add(SubNode2);
TreeNode SubNode3=new TreeNode("天津市",1,1);
RootNode.Nodes.Add(SubNode3);
TreeNode SubNode4=new TreeNode("重庆市",1,1);
RootNode.Nodes.Add(SubNode4);
TreeNode SubNode5=new TreeNode("渝中区",1,1);
SubNode4.Nodes.Add(SubNode5);
TreeNode SubNode6=new TreeNode("长寿区",1,1);
SubNode4.Nodes.Add(SubNode6);
SubNode6.SelectedImageIndex=1;
SubNode6.Expand();
}
p rivate void button4_Click(object sender, System.EventArgs e)
{//关闭应用程序
this.Close();
}
}
}