新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
C#編寫(xiě)操作TreeView組件剖析
C#語(yǔ)言有很多值得學(xué)習(xí)的地方,這里我們主要介紹C#編寫(xiě)操作TreeView組件,包括介紹加入子節(jié)點(diǎn)、兄弟節(jié)點(diǎn)、刪除節(jié)點(diǎn)、折疊、展開(kāi)等操作等方面。

創(chuàng)新互聯(lián)公司是工信部頒發(fā)資質(zhì)IDC服務(wù)器商,為用戶(hù)提供優(yōu)質(zhì)的成都服務(wù)器托管服務(wù)
C#編寫(xiě)操作TreeView組件的例子:
下面是C#編寫(xiě)操作TreeView組件的例子,在這個(gè)例子中,結(jié)合以上介紹的常用方法和一般方法,基本覆蓋來(lái)TreeView組件的一些最常用的操作。譬如可以靈活的程序中的TreeView組件中,進(jìn)行加入子節(jié)點(diǎn)、兄弟節(jié)點(diǎn)、刪除節(jié)點(diǎn)、折疊、展開(kāi)等操作。其中前三種基本操作是通過(guò)程序中彈出菜單中的功能來(lái)實(shí)現(xiàn)的,后面操作是通過(guò)程序中的按鈕來(lái)實(shí)現(xiàn)的。下面是此程序的代碼節(jié)略(TreeView.cs):
- using System ;
- using System.Drawing ;
- using System.Collections ;
- using System.ComponentModel ;
- using System.Windows.Forms ;
- using System.Data ;
- namespace 全面掌握TreeView組件的使用方法
- {
- /// Form1 的摘要說(shuō)明。
- public class Form1 : Form
- {
- private TreeView treeView1 ;
- private Button button1 ;
- private Button button2 ;
- private Button button3 ;
- private MenuItem menuItem2 ;
- private MenuItem menuItem3 ;
- private MenuItem menuItem4 ;
- private ContextMenu contextMenu1 ;
- private TextBox textBox1 ;
- private Label label1 ;
- /// 必需的設(shè)計(jì)器變量。
- private System.ComponentModel.Container components = null ;
- public Form1 ( )
- {
- //初始化窗體中的組件
- InitializeComponent ( ) ;
- }
- /// 清理所有正在使用的資源。
- protected override void Dispose ( bool disposing )
- {
- if ( disposing )
- {
- if ( components != null )
- {
- components.Dispose ( ) ;
- }
- }
- base.Dispose ( disposing ) ;
- }
- private void InitializeComponent ( )
- {
- //初始化代碼(略)
- }
- [ STAThread ]
- static void Main ( )
- {
- Application.Run ( new Form1 ( ) ) ;
- }
- private void AddChildNode ( )
- {
- //首先判斷是否選定組件中的位置
- if ( treeView1.SelectedNode == null )
- {
- MessageBox.Show ( "請(qǐng)選擇一個(gè)節(jié)點(diǎn)" , "提示信息" ,
MessageBoxButtons.OK , MessageBoxIcon.Information ) ;- }
- else
- {
- if ( textBox1.Text != "" )
- {
- //創(chuàng)建一個(gè)節(jié)點(diǎn)對(duì)象,并初始化
- TreeNode tmp ;
- tmp = new TreeNode ( textBox1.Text ) ;
- //在TreeView組件中加入子節(jié)點(diǎn)
- treeView1.SelectedNode.Nodes.Add ( tmp ) ;
- treeView1.SelectedNode = tmp ;
- treeView1.ExpandAll ( ) ;
- }
- else
- {
- MessageBox.Show ( "TextBox組件必須填入節(jié)點(diǎn)名稱(chēng)!" , "提示信息" ,
MessageBoxButtons.OK , MessageBoxIcon.Information ) ;- return ;
- }
- }
- }
- private void AddParent ( )
- {
- //首先判斷是否選定組件中節(jié)點(diǎn)的位置
- if ( treeView1.SelectedNode == null )
- {
- MessageBox.Show ( "請(qǐng)選擇一個(gè)節(jié)點(diǎn)" , "提示信息" ,
MessageBoxButtons.OK , MessageBoxIcon.Information ) ;- }
- else
- {
- if ( textBox1.Text != "" )
- {
- //創(chuàng)建一個(gè)節(jié)點(diǎn)對(duì)象,并初始化
- TreeNode tmp ;
- tmp = new TreeNode ( textBox1.Text ) ;
- //在TreeView組件中加入兄弟節(jié)點(diǎn)
- treeView1.SelectedNode.Parent.Nodes.Add ( tmp ) ;
- treeView1.ExpandAll ( ) ;
- }
- else
- {
- MessageBox.Show ( "TextBox組件必須填入節(jié)點(diǎn)名稱(chēng)!" , "提示信息" ,
MessageBoxButtons.OK , MessageBoxIcon.Information ) ;- return ;
- }
- }
- TreeNode tnode = new TreeNode ( textBox1.Text ) ;
- }
- private void treeView1_MouseDown ( object sender , MouseEventArgs e )
- {
- if ( e.Button == MouseButtons.Right )
- contextMenu1.Show ( this , new Point ( e.X , e.Y ) ) ;
- }
- private void button1_Click ( object sender , System.EventArgs e )
- {
- treeView1.SelectedNode.Expand ( ) ;
- }
- private void menuItem2_Click ( object sender , System.EventArgs e )
- {
- AddChildNode ( ) ;
- }
- private void menuItem3_Click ( object sender , System.EventArgs e )
- {
- AddParent ( ) ;
- }
- private void menuItem4_Click ( object sender , System.EventArgs e )
- {
- //判斷選定的節(jié)點(diǎn)是否存在下一級(jí)節(jié)點(diǎn)
- if ( treeView1.SelectedNode.Nodes.Count == 0 )
- //刪除節(jié)點(diǎn)
- treeView1.SelectedNode.Remove ( ) ;
- else
- MessageBox.Show ( "請(qǐng)先刪除此節(jié)點(diǎn)中的子節(jié)點(diǎn)!" , "提示信息" ,
MessageBoxButtons.OK , MessageBoxIcon.Information ) ;- }
- private void button2_Click ( object sender , System.EventArgs e )
- {
- //定位根節(jié)點(diǎn)
- treeView1treeView1.SelectedNode = treeView1.Nodes [ 0 ] ;
- //展開(kāi)組件中的所有節(jié)點(diǎn)
- treeView1.SelectedNode.ExpandAll ( ) ;
- }
- private void button3_Click ( object sender , System.EventArgs e )
- {
- //定位根節(jié)點(diǎn)
- treeView1treeView1.SelectedNode = treeView1.Nodes [ 0 ] ;
- //折疊組件中所有節(jié)點(diǎn)
- treeView1.SelectedNode.Collapse ( ) ;
- }
- }
- }
【編輯推薦】
- C#窗體里調(diào)用淺談
- C#調(diào)用ListEmployee命令
- C# CreateEmployeeDefinition()函數(shù)
- C#實(shí)現(xiàn)斷點(diǎn)續(xù)傳詳細(xì)剖析
- C# Employee對(duì)象淺談
網(wǎng)頁(yè)題目:C#編寫(xiě)操作TreeView組件剖析
URL標(biāo)題:http://www.5511xx.com/article/djhdeee.html


咨詢(xún)
建站咨詢(xún)
