直播中
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
namespace DynamicControls
{
public class Form1 : Form
{
private Button btnAdd ;
private System.ComponentModel.Container components = null ;
private Button txtAdd ;
//給產(chǎn)生的按鈕定義一個(gè)數(shù)量計(jì)算器
private int counter ;
//給產(chǎn)生的按鈕定義相對(duì)位置的縱坐標(biāo)
private int locY ;
//給產(chǎn)生的文本框定義一個(gè)數(shù)量計(jì)算器
private int counter01 ;
//給產(chǎn)生的文本框定義相對(duì)位置的縱坐標(biāo)
private int locY1 ;
public Form1 ( )
{
InitializeComponent ( ) ;
//初始化產(chǎn)生的按鈕何文本框位置的縱坐標(biāo)
locY = this.btnAdd.Location.Y ;
locY1 = this.txtAdd.Location.Y ;
}
//清除在程序中使用到的資源
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#中如何動(dòng)態(tài)產(chǎn)生組件!" ;
this.ResumeLayout ( false ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void btnAdd_Click ( object sender , System.EventArgs e )
{
//按鈕數(shù)量計(jì)算器在每次按鈕按動(dòng)后加"1"
counter += 1 ;
//對(duì)要產(chǎn)生的按鈕的縱坐標(biāo)的相對(duì)位置是前一個(gè)產(chǎn)生按鈕的相對(duì)位置的縱坐標(biāo)加"3"
locY += this.btnAdd.Height + 3 ;
//創(chuàng)建一個(gè)新的Button組件
Button myButton = new Button ( ) ;
//設(shè)定他的名稱和Text屬性,以及產(chǎn)生的位置
myButton.Name = "Button " + counter ;
myButton.Text = "按鈕 " + counter ;
myButton.Location = new Point ( btnAdd.Location.X , locY ) ;
//為產(chǎn)生的新的Button組件設(shè)定事件,本文中為產(chǎn)生的按鈕設(shè)定了三個(gè)事件
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 )
{
//文本框數(shù)量計(jì)算器在每次按鈕按動(dòng)后加"1"
counter01 += 1 ;
//對(duì)要產(chǎn)生的文本框的縱坐標(biāo)的相對(duì)位置是前一個(gè)產(chǎn)生按鈕的相對(duì)位置的縱坐標(biāo)加"3
locY1 += this.txtAdd.Height + 3 ;
//創(chuàng)建一個(gè)新的TextBox組件
TextBox myBox = new TextBox ( ) ;
//設(shè)定他的名稱和Text屬性,以及產(chǎn)生的位置
myBox.Name = "TextBox " + counter01 ;
myBox.Text = "文本框 " + counter01 ;
myBox.Location = new Point ( txtAdd.Location.X , locY1 ) ;
//為產(chǎn)生的新的TextBox組件設(shè)定事件,本文中為產(chǎn)生的文本框設(shè)定了一個(gè)事件
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 ;
//設(shè)定按鈕的背景色
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 + "被按動(dòng)了!");
}
else
{
TextBox control = ( TextBox ) sender ;
MessageBox.Show ( control.Text + "被按動(dòng)了!" ) ;
}
}
}
}
五. 總結(jié):
通過(guò)上面介紹,不難看出,動(dòng)態(tài)創(chuàng)建組件并不是一件很難的事情,難就難在為這個(gè)組件創(chuàng)建事件上面,因?yàn)檫@涉及到實(shí)值類型變量和參考類型變量的轉(zhuǎn)換,這就是所謂的裝箱和出箱的問(wèn)題。當(dāng)然在程序設(shè)計(jì)的時(shí)候,你不僅可以創(chuàng)建那些可見(jiàn)的組件,也可以創(chuàng)建那些不可見(jiàn)的組件,具體的實(shí)現(xiàn)方法和本文中的方法類似。