//把所有按钮添加到List集合
List<Control> buttonList = new List<Control>();
foreach (Control con in groupBox1.Controls)//查找groupBox1里面的button控件
{
//判断控件类型是否为按钮
if (con is Button)
{
buttonList.Add(con);
}
}
//查找所有List,设定属性
for (int i = 0; i < buttonList.Count; i++)
{
Button btn = buttonList[i] as Button;
btn.Text = "输入" + i;
}
如上图修改哪个控件里面的按钮名称就把第二段代码里面的groupBox1改成什么,比如panel1等等。
Button[] inbtn = new Button[32];
inbtn = new Button[] { button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15,button16 };
//设定属性
for (int i = 0; i < 16; i++)
{
Button btn = inbtn[i] as Button;
btn.Text = "输入"+i;
}
也可以把按钮放入数组里面更改text值。