c#动态生成按钮数组,并能产生事件
各位大侠好!
求教个问题:
c#动态生成 【56,3】个button数组。并且点击对应的按钮时 ,能产生相应的事件!紧急 。谢谢大侠!
如何在winForm中实现
--------------------编程问答-------------------- 动态添加3个按钮,单击对应按钮,在窗体标题栏显示按钮的名称:
private void Form1_Load(object sender, EventArgs e)--------------------编程问答-------------------- 在 clickHanlder 里修改对应的处理。
{
Button[] buttons = new Button[3];
for (int i = 0; i < buttons.Length; i++)
{
buttons[i] = new Button();
buttons[i].Name = "button" + i;
buttons[i].Text = buttons[i].Name;
buttons[i].Location = new Point(10, 30 * i);
buttons[i].Click += new EventHandler(Buttons_Click);
}
this.Controls.AddRange(buttons);
}
void Buttons_Click(object sender, EventArgs e)
{
this.Text = (sender as Button).Text;
}
private void Form1_Load(object sender, EventArgs e)
{
this.Width = 350;
this.Height = 250;
int btnWidth = 30;
int btnHeight = 20;
int m = 9;
int n = 9;
EventHandler clickHandler = (s, evt) => {
var btn = (Button)s;
var p = (Point)btn.Tag;
MessageBox.Show(string.Format("This is the [{0},{1}] button click handler.", p.X, p.Y));
};
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
var btn = new Button();
btn.Text = string.Format("{0}{1}", i, j);
btn.Name = "button_" + i + "_" + j;
btn.Click += clickHandler;
btn.Width = btnWidth;
btn.Height = btnHeight;
btn.Tag = new Point(i, j);
btn.Location = new Point(j*btnWidth, i*btnHeight);
this.Controls.Add(btn);
}
}
}
补充:.NET技术 , C#