黑马程序员技术交流社区
标题: C# 中的动态创建组件(属性及事件)的实现思路及方法 [打印本页]
作者: 陈君 时间: 2014-8-18 17:44
标题: C# 中的动态创建组件(属性及事件)的实现思路及方法
这篇文章主要介绍了C# 中的动态创建组件,有需要的朋友可以参考一下
一. Boxing (装箱)和Unboxing (出箱):
在用Visual C#动态创建组件的时候,要涉及到二种数据类型变量的转换,这二种类型变量就是实值类型(Value Type)变量和参考类型(Reference Type)变量,而这种转换过程在Visual C#中被称为Boxing (装箱)和Unboxing (出箱)。其中把实值类型变量转换成参考类型变量就是Boxing (装箱);把参考类型变量转换成实值类型变量就是Unboxing (出箱)。那么什么是实值类型,说的简单些,就是我们平常使用的整型、布尔型、枚举型等,这些类型的变量就是实值类型变量了;所谓参考类型,在Visual C#中指的就是Object、Class、Interface、Delegate、String、Array等,他和实值类型最主要的不同之处就是,参考类型变量存放的是指向实体对象的指针,而实值类型变量却是实实在在地实体对象。在本文介绍的程序中,主要涉及的是出箱。具体的处理方法,在下面有着具体介绍。
二. 程序设计中的关键步骤以及解决方法:
1).如何在窗体上创建Button组件:
其实用Visual C#创建一个组件是十分方便的,只用下列二行语句就可以完成了:
- //创建一个新的Button组件
- Button myButton = new Button ( ) ;
- //在窗体中显示此按钮
- this.Controls.Add ( myButton ) ;
复制代码但此时创建的这个Button组件没有任何属性,并且也没有任何事件,在本文中介绍的程序中创建的Button组件,不仅有属性也有事件,下列语句就是本文程序创建Button组件源代码:
- //按钮数量计算器在每次按钮按动后加 "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 btn_MouseEnter ( object sender , System.EventArgs e )
- {
- //出箱
- Button currentButton = ( Button ) sender ;
- //设定按钮的背景色
- currentButton.BackColor = Color.Red ;
- }
复制代码
其他事件可以仿照此事件的处理过程来处理。
(3). 如何在窗体上创建TextBox组件:
创建TextBox组件的过程和创建Button组件过程相类似,只是在创建的组件类型上面有一点区别,具体实现语句如下:
- //文本框数量计算器在每次按钮按动后加 "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 ) ;
复制代码
此时细心的读者又会发现,为每一个TextBox组件创建Click事件和为Button组件创建的Click事件也是一样的,这样在Click事件中不仅要判断是哪个组件触发了事件,还要判断是那种类型的组件触发了事件,下面语句是实现这些判断地具体方法:
- 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 + "被按动了! " ) ;
- }
- }
复制代码
当然如果你也可以单独为TextBox组件创建Click事件。此时创建的事件语句可改为:
- myBox.Click += new System.EventHandler ( this.txt _Click ) ;
- //下面是实现txt _Click ( )事件的程序代码:
- private void txt_Click ( object sender , System.EventArgs e )
- {
- TextBox currentButton = ( TextBox ) sender ;
- MessageBox.Show ( currentButton.Text + "被按动了! ");
- }
复制代码
下面是实现上面结果的程序源代码:
- 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 ;
- //给产生的按钮定义一个数量计算器
- private int counter ;
- //给产生的按钮定义相对位置的纵坐标
- private int locY ;
- //给产生的文本框定义一个数量计算器
- private int counter01 ;
- //给产生的文本框定义相对位置的纵坐标
- private int locY1 ;
- public Form1 ( )
- {
- InitializeComponent ( ) ;
- //初始化产生的按钮何文本框位置的纵坐标
- 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#中如何动态产生组件! " ;
- 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 + "被按动了! " ) ;
- }
- }
- }
- }
复制代码
| 欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |