批量给控件赋值
方法一:利用Controls.Find方法
方法二:利用反射(该方法可批量给控件任何属性赋值)
方法一:利用Controls.Find方法:
-
for(int i = 0 , i < 10 , i++)
-
{
-
Label newLabel = (System.Windows.Forms.Label)this.Controls.Find("label1" + i.toString(),false)[0];
-
newLabel .Text = i.toString();
-
}
方法二:利用反射(该方法可批量给控件任何属性赋值)
-
for(int i = 0 , i < 10 , i++)
-
{
-
//取类型
- System.Type type = typeof(System.Windows.Forms.Label);
- //取属性名称
- System.Reflection.PropertyInfo p = type.GetProperty("Text");
- //赋值
- p.SetValue((System.Windows.Forms.Label)this.Controls.Find("label" + i.toString() , false)[0] , i.toString() , null);
共享事件
、其中数字按钮(0-9)相应的click事件,其中事件的实现是要在textbox中显示所按下的按钮的数字值。
我们只需为按钮7实现一个鼠标点击事件,代码如下:
1
2
3
4
5
6
7
8
9
10
11
|
private
void
b_7_Click(
object
sender, EventArgs e)
{
/*
* 按钮7的click事件
*
* */
Button bt = (Button)sender;
//获得触发click事件的对象,并转换成button类型
this
.inputNum(bt.Text);
//把button对象的值text作为参数传进inputNum
}
|