C# 中的动态创建组件(属性及事件)的实现思路及方法
//清除在程序中使用到的资源 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 + "被按动了! " ) ; } }
} }
- C# 动态创建组件(属性及事件)
- PHP使用方法重载实现动态创建属性的get和set方法
- 用 C# 做组件设计时的事件实现方法讨论
- Vue实现调节窗口大小时触发事件动态调节更新组件尺寸的方法
- c# BackgroundWorker组件介绍(属性、方法、事件)
- PHP使用方法重载实现动态创建属性的get和set方法
- 用 C# 做组件设计时的事件实现方法讨论
- PHP使用方法重载实现动态创建属性的get和set方法
- C#动态对象(dynamic)示例(实现方法和属性的动态)
- C#动态对象(dynamic)示例(实现方法和属性的动态)
- 用 C# 做组件设计时的事件实现方法讨论
- Javascript 创建类并动态添加属性及方法的简单实现
- C#动态对象(dynamic)示例(实现方法和属性的动态)
- c# BackgroundWorker组件介绍(属性、方法、事件)
- C#动态对象(dynamic)示例(实现方法和属性的动态)
- MFC中动态创建控件以及事件响应实现方法
- C#中事件的动态调用实现方法
- C# 使用反射实现根据名称动态创建窗体的几种方法
- c# BackgroundWorker组件介绍(属性、方法、事件)
- C#在运行时动态创建类型的实现方法