通常在寫程序的時候,當要用到某些組件,采用的方法一般都是動態創建,用完以后就釋放掉。Visual C#在程序運行的時候也可以動態創建組件,下面就結合一個程序例子來具體介紹如何用Visual C#動態生成組件。首先讓我們了解一下,在動態創建組件的過程中要用到的一些概論和理論。
一. Boxing (裝箱)和Unboxing (出箱):
在用Visual C#動態創建組件的時候,要涉及到二種數據類型變量的轉換,這二種類型變量就是實值類型(Value Type)變量和參考類型(Reference Type)變量,而這種轉換過程在Visual C#中被稱為Boxing (裝箱)和Unboxing (出箱)。其中把實值類型變量轉換成參考類型變量就是Boxing (裝箱);把參考類型變量轉換成實值類型變量就是Unboxing (出箱)。那么什么是實值類型,說的簡單些,就是我們平常使用的整型、布爾型、枚舉型等,這些類型的變量就是實值類型變量了;所謂參考類型,在Visual C#中指的就是Object、Class、Interface、Delegate、String、Array等,他和實值類型最主要的不同之處就是,參考類型變量存放的是指向實體對象的指針,而實值類型變量卻是實實在在地實體對象。在本文介紹的程序中,主要涉及的是出箱。具體的處理方法,在下面有著具體介紹。
二. 程序設計中的關鍵步驟以及解決方法:
文中軟件主要功能是用通過窗體上的二個按鈕來創建二個不同類型的WinForm組件--Button組件和TextBox組件,并在創建的同時為每一個組件的屬性賦值,給每一個創建的組件也創建了事件。
1).如何在窗體上創建Button組件:
其實用Visual C#創建一個組件是十分方便的,只用下列二行語句就可以完成了:
//下面是實現txt _Click ( )事件的程序代碼:
private void txt_Click ( object sender , System.EventArgs e )
{
TextBox currentButton = ( TextBox ) sender ;
MessageBox.Show ( currentButton.Text + "被按動了! ");
}
下面是實現上面結果的程序源代碼:
//清除在程序中使用到的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
private void InitializeComponent ( )
{
this.btnAdd = new Button ( ) ;
this.txtAdd = new Button ( ) ;
this.SuspendLayout ( ) ;
this.btnAdd.FlatStyle = FlatStyle.Popup ;
this.btnAdd.Location = new System.Drawing.Point ( 8 , 16 ) ;
this.btnAdd.Name = "btnAdd " ;
this.btnAdd.TabIndex = 0 ;
this.btnAdd.Text = "生成按鈕! " ;
this.btnAdd.Click += new System.EventHandler ( this.btnAdd_Click ) ;
this.txtAdd.FlatStyle = FlatStyle.Popup ;
this.txtAdd.Location = new System.Drawing.Point ( 108 , 16 ) ;
this.txtAdd.Name = "txtAdd " ;
this.txtAdd.TabIndex = 1 ;
this.txtAdd.Text = "生成文本框! " ;
this.txtAdd.Click += new System.EventHandler ( this.txtAdd_Click ) ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;
this.Controls.Add ( btnAdd ) ;
this.Controls.Add ( txtAdd ) ;
this.Name = "Form1 " ;
this.Text = "在Visual C#中如何動態產生組件! " ;
this.ResumeLayout ( false ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void btnAdd_Click ( object sender , System.EventArgs e )
{
//按鈕數量計算器在每次按鈕按動后加 "1 "
counter += 1 ;
//對要產生的按鈕的縱坐標的相對位置是前一個產生按鈕的相對位置的縱坐標加 "3 "
locY += this.btnAdd.Height + 3 ;
//創建一個新的Button組件
Button myButton = new Button ( ) ;
//設定他的名稱和Text屬性,以及產生的位置
myButton.Name = "Button " + counter ;
myButton.Text = "按鈕 " + counter ;
myButton.Location = new Point ( btnAdd.Location.X , locY ) ;
//為產生的新的Button組件設定事件,本文中為產生的按鈕設定了三個事件
myButton.MouseEnter += new System.EventHandler ( this.btn_MouseEnter ) ;
myButton.MouseLeave += new System.EventHandler ( this.btn_MouseLeave ) ;
myButton.Click += new System.EventHandler ( this.btn_Click ) ;
//在窗體中顯示此按鈕
this.Controls.Add ( myButton ) ;
}
private void txtAdd_Click ( object sender , System.EventArgs e )
{
//文本框數量計算器在每次按鈕按動后加 "1 "
counter01 += 1 ;
//對要產生的文本框的縱坐標的相對位置是前一個產生按鈕的相對位置的縱坐標加 "3
locY1 += this.txtAdd.Height + 3 ;
//創建一個新的TextBox組件
TextBox myBox = new TextBox ( ) ;
//設定他的名稱和Text屬性,以及產生的位置
myBox.Name = "TextBox " + counter01 ;
myBox.Text = "文本框 " + counter01 ;
myBox.Location = new Point ( txtAdd.Location.X , locY1 ) ;
//為產生的新的TextBox組件設定事件,本文中為產生的文本框設定了一個事件
myBox.Click += new System.EventHandler ( this.btn_Click ) ;
//在窗體中顯示此文本框
this.Controls.Add ( myBox ) ;
}
private void btn_MouseEnter ( object sender , System.EventArgs e )
{
//出箱
Button currentButton = ( Button ) sender ;
//設定按鈕的背景色
currentButton.BackColor = Color.Red ;
}
private void btn_MouseLeave ( object sender , System.EventArgs e )
{
//出箱
Button currentButton = ( Button ) sender ;
currentButton.BackColor = Control.DefaultBackColor ;
}
private void btn_Click ( object sender , System.EventArgs e )
{
if ( sender.GetType ( ) == typeof ( Button ) )
{
Button control = ( Button ) sender ;
MessageBox.Show ( control.Text + "被按動了! ");
}
else
{
TextBox control = ( TextBox ) sender ;
MessageBox.Show ( control.Text + "被按動了! " ) ;
}
}
}
}
新聞熱點
疑難解答