詳解C#的WinForm增加treeView1控件
作者:佚名
在這里介紹在C#的WinForm中增加一個(gè)treeView1控件的方法,希望能對(duì)大家有所幫助。
Winform中關(guān)于treeView控件的做法,大家都比較了解。本文將談到的是treeView1控件的增加過(guò)程,希望對(duì)廣大開(kāi)發(fā)人員有所幫助。
使用TreeNode的tag屬性放置編碼,TreeNode的Text屬性放置名稱,最簡(jiǎn)單的示例如下——先添加兩個(gè)節(jié)點(diǎn),然后處理AfterSelect事件:
- using System;
- using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;
- namespace testtree
- {
- ///
- /// Form1 的摘要說(shuō)明。
- ///
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.TreeView treeView1;
- ///
- /// 必需的設(shè)計(jì)器變量。
- ///
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- //
- // Windows窗體設(shè)計(jì)器支持所必需的
- //
- InitializeComponent();
- //
- // TODO:在InitializeComponent調(diào)用后添加任何構(gòu)造函數(shù)代碼
- //
- TreeNode tn = new TreeNode();
- tn.Tag = "1001"; tn.Text = "hello";
- TreeNode tn2 = new TreeNode();
- tn2.Tag = "1002"; tn2.Text = "ok";
- tn.Nodes.Add(tn2);
- treeView1.Nodes.Add(tn);
- }
- ///
- /// 清理所有正在使用的資源。
- ///
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows窗體設(shè)計(jì)器生成的代碼
- ///
- ///設(shè)計(jì)器支持所需的方法-不要使用代碼編輯器修改
- /// 此方法的內(nèi)容。
- ///
- private void InitializeComponent()
- {
- this.treeView1 = new System.Windows.Forms.TreeView(); this.SuspendLayout();
- //
- // treeView1控件
- //
- this.treeView1.ImageIndex = -1; this.treeView1.Location = new System.Drawing.Point(16, 8); this.treeView1.Name = "treeView1"; this.treeView1.SelectedImageIndex = -1; this.treeView1.TabIndex = 0; this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.treeView1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false);
- }
- #endregion
- ///
- /// 應(yīng)用程序的主入口點(diǎn)。
- ///
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
- {
- string s = treeView1.SelectedNode.Tag.ToString(); string s2 = treeView1.SelectedNode.Text.ToString();
- }
- }
- }
關(guān)于C#的WinForm增加treeView1控件就總結(jié)到這里。
【編輯推薦】
責(zé)任編輯:彭凡
來(lái)源:
IT專家網(wǎng)