A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

这篇文章主要介绍了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#创建一个组件是十分方便的,只用下列二行语句就可以完成了:

  1. //创建一个新的Button组件
  2. Button myButton = new Button ( ) ;
  3. //在窗体中显示此按钮
  4. this.Controls.Add ( myButton ) ;
复制代码
但此时创建的这个Button组件没有任何属性,并且也没有任何事件,在本文中介绍的程序中创建的Button组件,不仅有属性也有事件,下列语句就是本文程序创建Button组件源代码:

  1. //按钮数量计算器在每次按钮按动后加 "1 "
  2. counter += 1 ;
  3. //对要产生的按钮的纵坐标的相对位置是前一个产生按钮的相对位置的纵坐标加 "3 "
  4. locY += this.btnAdd.Height + 3 ;
  5. //创建一个新的Button组件
  6. Button myButton = new Button ( ) ;
  7. //设定他的名称和Text属性,以及产生的相对位置
  8. myButton.Name = "Button " + counter ;
  9. myButton.Text = "按钮 " + counter ;
  10. myButton.Location = new Point ( btnAdd.Location.X , locY ) ;
  11. //为产生的新的Button组件设定事件,本文中为产生的按钮设定了三个事件
  12. myButton.MouseEnter += new System.EventHandler ( this.btn_MouseEnter ) ;
  13. myButton.MouseLeave += new System.EventHandler ( this.btn_MouseLeave ) ;
  14. myButton.Click += new System.EventHandler ( this.btn_Click ) ;
  15. //在窗体中显示此按钮
  16. this.Controls.Add ( myButton ) ;
复制代码

  1. private void btn_MouseEnter ( object sender , System.EventArgs e )
  2. {
  3. //出箱
  4. Button currentButton = ( Button ) sender ;
  5. //设定按钮的背景色
  6. currentButton.BackColor = Color.Red ;
  7. }
复制代码

其他事件可以仿照此事件的处理过程来处理。
(3). 如何在窗体上创建TextBox组件:
创建TextBox组件的过程和创建Button组件过程相类似,只是在创建的组件类型上面有一点区别,具体实现语句如下:

  1. //文本框数量计算器在每次按钮按动后加 "1 "
  2. counter01 += 1 ;
  3. //对要产生的文本框的纵坐标的相对位置是前一个产生按钮的相对位置的纵坐标加 "3
  4. locY1 += this.txtAdd.Height + 3 ;
  5. //创建一个新的TextBox组件
  6. TextBox myBox = new TextBox ( ) ;
  7. //设定他的名称和Text属性,以及产生的位置
  8. myBox.Name = "TextBox " + counter01 ;
  9. myBox.Text = "文本框 " + counter01 ;
  10. myBox.Location = new Point ( txtAdd.Location.X , locY1 ) ;
  11. //为产生的新的TextBox组件设定事件,本文中为产生的文本框设定了一个事件
  12. myBox.Click += new System.EventHandler ( this.btn_Click ) ;
  13. //在窗体中显示此文本框
  14. this.Controls.Add ( myBox ) ;
复制代码

此时细心的读者又会发现,为每一个TextBox组件创建Click事件和为Button组件创建的Click事件也是一样的,这样在Click事件中不仅要判断是哪个组件触发了事件,还要判断是那种类型的组件触发了事件,下面语句是实现这些判断地具体方法:

  1. private void btn_Click ( object sender , System.EventArgs e )
  2. {
  3. if ( sender.GetType ( ) == typeof ( Button ) )
  4. {
  5. Button control = ( Button ) sender ;
  6. MessageBox.Show ( control.Text + "被按动了! ");
  7. }
  8. else
  9. {
  10. TextBox control = ( TextBox ) sender ;
  11. MessageBox.Show ( control.Text + "被按动了! " ) ;
  12. }
  13. }
复制代码

当然如果你也可以单独为TextBox组件创建Click事件。此时创建的事件语句可改为:

  1. myBox.Click += new System.EventHandler ( this.txt _Click ) ;
  2. //下面是实现txt _Click ( )事件的程序代码:
  3. private void txt_Click ( object sender , System.EventArgs e )
  4. {
  5. TextBox currentButton = ( TextBox ) sender ;
  6. MessageBox.Show ( currentButton.Text + "被按动了! ");
  7. }
复制代码

下面是实现上面结果的程序源代码:

  1. using System ;
  2. using System.Drawing ;
  3. using System.Collections ;
  4. using System.ComponentModel ;
  5. using System.Windows.Forms ;
  6. using System.Data ;
  7. namespace DynamicControls
  8. {
  9. public class Form1 : Form
  10. {
  11. private Button btnAdd ;
  12. private System.ComponentModel.Container components = null ;
  13. private Button txtAdd ;
  14. //给产生的按钮定义一个数量计算器
  15. private int counter ;
  16. //给产生的按钮定义相对位置的纵坐标
  17. private int locY ;
  18. //给产生的文本框定义一个数量计算器
  19. private int counter01 ;
  20. //给产生的文本框定义相对位置的纵坐标
  21. private int locY1 ;
  22. public Form1 ( )
  23. {
  24. InitializeComponent ( ) ;
  25. //初始化产生的按钮何文本框位置的纵坐标
  26. locY = this.btnAdd.Location.Y ;
  27. locY1 = this.txtAdd.Location.Y ;
  28. }
  29. //清除在程序中使用到的资源
  30. protected override void Dispose ( bool disposing )
  31. {
  32. if ( disposing )
  33. {
  34. if ( components != null )
  35. {
  36. components.Dispose ( ) ;
  37. }
  38. }
  39. base.Dispose ( disposing ) ;
  40. }
  41. private void InitializeComponent ( )
  42. {
  43. this.btnAdd = new Button ( ) ;
  44. this.txtAdd = new Button ( ) ;
  45. this.SuspendLayout ( ) ;
  46. this.btnAdd.FlatStyle = FlatStyle.Popup ;
  47. this.btnAdd.Location = new System.Drawing.Point ( 8 , 16 ) ;
  48. this.btnAdd.Name = "btnAdd " ;
  49. this.btnAdd.TabIndex = 0 ;
  50. this.btnAdd.Text = "生成按钮! " ;
  51. this.btnAdd.Click += new System.EventHandler ( this.btnAdd_Click ) ;
  52. this.txtAdd.FlatStyle = FlatStyle.Popup ;
  53. this.txtAdd.Location = new System.Drawing.Point ( 108 , 16 ) ;
  54. this.txtAdd.Name = "txtAdd " ;
  55. this.txtAdd.TabIndex = 1 ;
  56. this.txtAdd.Text = "生成文本框! " ;
  57. this.txtAdd.Click += new System.EventHandler ( this.txtAdd_Click ) ;
  58. this.AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
  59. this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;
  60. this.Controls.Add ( btnAdd ) ;
  61. this.Controls.Add ( txtAdd ) ;
  62. this.Name = "Form1 " ;
  63. this.Text = "在Visual C#中如何动态产生组件! " ;
  64. this.ResumeLayout ( false ) ;
  65. }
  66. static void Main ( )
  67. {
  68. Application.Run ( new Form1 ( ) ) ;
  69. }
  70. private void btnAdd_Click ( object sender , System.EventArgs e )
  71. {
  72. //按钮数量计算器在每次按钮按动后加 "1 "
  73. counter += 1 ;
  74. //对要产生的按钮的纵坐标的相对位置是前一个产生按钮的相对位置的纵坐标加 "3 "
  75. locY += this.btnAdd.Height + 3 ;
  76. //创建一个新的Button组件
  77. Button myButton = new Button ( ) ;
  78. //设定他的名称和Text属性,以及产生的位置
  79. myButton.Name = "Button " + counter ;
  80. myButton.Text = "按钮 " + counter ;
  81. myButton.Location = new Point ( btnAdd.Location.X , locY ) ;
  82. //为产生的新的Button组件设定事件,本文中为产生的按钮设定了三个事件
  83. myButton.MouseEnter += new System.EventHandler ( this.btn_MouseEnter ) ;
  84. myButton.MouseLeave += new System.EventHandler ( this.btn_MouseLeave ) ;
  85. myButton.Click += new System.EventHandler ( this.btn_Click ) ;
  86. //在窗体中显示此按钮
  87. this.Controls.Add ( myButton ) ;
  88. }
  89. private void txtAdd_Click ( object sender , System.EventArgs e )
  90. {
  91. //文本框数量计算器在每次按钮按动后加 "1 "
  92. counter01 += 1 ;
  93. //对要产生的文本框的纵坐标的相对位置是前一个产生按钮的相对位置的纵坐标加 "3
  94. locY1 += this.txtAdd.Height + 3 ;
  95. //创建一个新的TextBox组件
  96. TextBox myBox = new TextBox ( ) ;
  97. //设定他的名称和Text属性,以及产生的位置
  98. myBox.Name = "TextBox " + counter01 ;
  99. myBox.Text = "文本框 " + counter01 ;
  100. myBox.Location = new Point ( txtAdd.Location.X , locY1 ) ;
  101. //为产生的新的TextBox组件设定事件,本文中为产生的文本框设定了一个事件
  102. myBox.Click += new System.EventHandler ( this.btn_Click ) ;
  103. //在窗体中显示此文本框
  104. this.Controls.Add ( myBox ) ;
  105. }
  106. private void btn_MouseEnter ( object sender , System.EventArgs e )
  107. {
  108. //出箱
  109. Button currentButton = ( Button ) sender ;
  110. //设定按钮的背景色
  111. currentButton.BackColor = Color.Red ;
  112. }
  113. private void btn_MouseLeave ( object sender , System.EventArgs e )
  114. {
  115. //出箱
  116. Button currentButton = ( Button ) sender ;
  117. currentButton.BackColor = Control.DefaultBackColor ;
  118. }
  119. private void btn_Click ( object sender , System.EventArgs e )
  120. {
  121. if ( sender.GetType ( ) == typeof ( Button ) )
  122. {
  123. Button control = ( Button ) sender ;
  124. MessageBox.Show ( control.Text + "被按动了! ");
  125. }
  126. else
  127. {
  128. TextBox control = ( TextBox ) sender ;
  129. MessageBox.Show ( control.Text + "被按动了! " ) ;
  130. }
  131. }
  132. }
  133. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马