樹視圖TreeView 屬性及方法
屬性
說明
Nodes
獲取分配給樹視圖控件的樹節點集合
PathSeparator
獲取或設置樹節點路徑所使用的分隔符串
SelesctedNode
獲取或設置當樹節點選定時所使用的圖像列表索引值
ShowNodeToolTips
獲取或設置一個值,用以指示樹圖中的樹節點是否經過排序
VisibleCount
獲取樹視圖控件黃總完全可見的樹節點數目
CollapseAll
折疊所有的樹節點
ExpandAll
展開所有的樹節點
GetNodeAt
檢索位於指定位置的 樹節點
GetNodeCount
檢索分配給樹視圖控件的樹節點數
............................
..................................................................................................................................................................
事件
說明
AfterCheck
在選中樹節點復選框後 發生
AfterCollapse
在選中樹節點復選框後發生
AfterExpand
在展開樹節點後發生
AfterSelect
在選定樹節點後發生
NodeMouseClick
當用戶使用鼠標單擊TreeView 時發生
NodeMouseDoubleClick
當用戶使用鼠標雙擊TreeNode 時發生
Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace TreeViewTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { treeView1.ContextMenuStrip=contextMenuStrip1; //設置樹空間的快捷菜單 TreeNode TopNode=treeView1.Nodes.Add("博主"); //建立第一個頂級節點 //建立4個基礎節點 ,分別表示 4個大的分支 TreeNode ParentNode1=new TreeNode("家人"); TreeNode ParentNode2=new TreeNode("朋友"); TreeNode ParentNode3=new TreeNode("老師"); TreeNode ParentNode4=new TreeNode("同學"); //將四個基礎節點添加到頂級節點中 TopNode.Nodes.Add(ParentNode1); TopNode.Nodes.Add(ParentNode2); TopNode.Nodes.Add(ParentNode3); TopNode.Nodes.Add(ParentNode4); //建立6個子節點,分別表示6個小的分支 TreeNode ChildNode1=new TreeNode("ShinePans"); TreeNode ChildNode2=new TreeNode("爸爸"); TreeNode ChildNode3=new TreeNode("媽媽"); TreeNode ChildNode4=new TreeNode("xuzhengmao"); TreeNode ChildNode5=new TreeNode("秦明"); TreeNode ChildNode6=new TreeNode("zhangyihui"); TreeNode ChildNode7=new TreeNode("xuzhengmao"); TreeNode ChildNode8=new TreeNode("zhangyihui"); ParentNode1.Nodes.Add(ChildNode1); ParentNode1.Nodes.Add(ChildNode2); ParentNode1.Nodes.Add(ChildNode3); ParentNode4.Nodes.Add(ChildNode4); ParentNode3.Nodes.Add(ChildNode5); ParentNode4.Nodes.Add(ChildNode6); ParentNode2.Nodes.Add(ChildNode7); ParentNode2.Nodes.Add(ChildNode8); } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { label1.Text="選擇的部門:"+e.Node.Text; } private void 全部展開ToolStripMenuItem_Click(object sender, EventArgs e) { treeView1.ExpandAll(); } private void 全部折疊ToolStripMenuItem_Click(object sender, EventArgs e) { treeView1.CollapseAll(); } } }
program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace TreeViewTest { static class Program { ////// 應用程序的主入口點。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); //樹視圖 } } }
Form1設計
namespace TreeViewTest { partial class Form1 { ////// 必需的設計器變量。 /// private System.ComponentModel.IContainer components = null; ////// 清理所有正在使用的資源。 /// /// 如果應釋放托管資源,為 true;否則為 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設計器生成的代碼 ////// 設計器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內容。 /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.treeView1 = new System.Windows.Forms.TreeView(); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.label1 = new System.Windows.Forms.Label(); this.全部展開ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.全部折疊ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.contextMenuStrip1.SuspendLayout(); this.SuspendLayout(); // // treeView1 // this.treeView1.Location = new System.Drawing.Point(5, 7); this.treeView1.Name = "treeView1"; this.treeView1.Size = new System.Drawing.Size(247, 294); this.treeView1.TabIndex = 0; this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect); // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.全部展開ToolStripMenuItem, this.全部折疊ToolStripMenuItem}); this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.Size = new System.Drawing.Size(153, 70); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(11, 320); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(0, 12); this.label1.TabIndex = 2; // // 全部展開ToolStripMenuItem // this.全部展開ToolStripMenuItem.Name = "全部展開ToolStripMenuItem"; this.全部展開ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.全部展開ToolStripMenuItem.Text = "全部展開"; this.全部展開ToolStripMenuItem.Click += new System.EventHandler(this.全部展開ToolStripMenuItem_Click); // // 全部折疊ToolStripMenuItem // this.全部折疊ToolStripMenuItem.Name = "全部折疊ToolStripMenuItem"; this.全部折疊ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.全部折疊ToolStripMenuItem.Text = "全部折疊"; this.全部折疊ToolStripMenuItem.Click += new System.EventHandler(this.全部折疊ToolStripMenuItem_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(260, 361); this.Controls.Add(this.label1); this.Controls.Add(this.treeView1); this.Name = "Form1"; this.Text = "樹視圖"; this.Load += new System.EventHandler(this.Form1_Load); this.contextMenuStrip1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TreeView treeView1; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.Label label1; private System.Windows.Forms.ToolStripMenuItem 全部展開ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 全部折疊ToolStripMenuItem; } }