RT。。 Button[] bt = new Button[100];
Button btStart = new Button();
private void Form1_Load(object sender, EventArgs e)
{
btStart.Location = new Point(12, 12);
btStart.Text = "Start";
btStart.Size = new Size(112, 50);
Controls.Add(btStart);//Start over Button bt100 = new Button();
bt100 = new Button();
bt100.Size = new Size(36, 29);
bt100.Location = new Point(364, 395);
bt100.Text = "100";
Controls.Add(bt100);
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
bt[i * 10 + j] = new Button();
bt[i * 10 + j].Size = new Size(36, 29);
bt[i * 10 + j].Location = new Point((4 + 40 * (j - 1)), (80 + 35 * i));//每行每列的排序
bt[i * 10 + j].Text = (i * 10 + j).ToString();
Controls.Add(bt[i * 10 + j]); }
}
}
然后我想用代码添加每个按钮的Click事件 试过网上好多种方法 都没用 在此跪求满意答案谢谢
Button btStart = new Button();
private void Form1_Load(object sender, EventArgs e)
{
btStart.Location = new Point(12, 12);
btStart.Text = "Start";
btStart.Size = new Size(112, 50);
Controls.Add(btStart);//Start over Button bt100 = new Button();
bt100 = new Button();
bt100.Size = new Size(36, 29);
bt100.Location = new Point(364, 395);
bt100.Text = "100";
Controls.Add(bt100);
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
bt[i * 10 + j] = new Button();
bt[i * 10 + j].Size = new Size(36, 29);
bt[i * 10 + j].Location = new Point((4 + 40 * (j - 1)), (80 + 35 * i));//每行每列的排序
bt[i * 10 + j].Text = (i * 10 + j).ToString();
Controls.Add(bt[i * 10 + j]); }
}
}
然后我想用代码添加每个按钮的Click事件 试过网上好多种方法 都没用 在此跪求满意答案谢谢
Button btStart = new Button();
private void Form1_Load(object sender, EventArgs e)
{
btStart.Location = new Point(12, 12);
btStart.Text = "Start";
btStart.Size = new Size(112, 50);
Controls.Add(btStart);//Start over Button bt100 = new Button();
bt100 = new Button();
bt100.Size = new Size(36, 29);
bt100.Location = new Point(364, 395);
bt100.Text = "100";
Controls.Add(bt100); int index = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
bt[index] = new Button();
bt[index].Size = new Size(36, 29);
bt[index].Location = new Point((4 + 40 * (j - 1)), (80 + 35 * i));//每行每列的排序
bt[index].Text = (i * 10 + j).ToString();
bt[index].Click += OnClick; index++;
}
} this.SuspendLayout();
Controls.AddRange(bt);
this.ResumeLayout();
} private void OnClick(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn != null)
{
MessageBox.Show(btn.Text);
}
}控件太多
可以使用AddRange来增加
如果是一样的话,可以先定义一个公用的btn_Click, 然后bt[i * 10 + j].Click += new EventHandler(btn_Click)就爱可以了。
Button[] bt = new Button[100];
Button btStart = new Button();
private void Form1_Load(object sender, EventArgs e)
{
btStart.Location = new Point(12, 12);
btStart.Text = "Start";
btStart.Size = new Size(112, 50);
Controls.Add(btStart);//Start over Button bt100 = new Button();
bt100 = new Button();
bt100.Size = new Size(36, 29);
bt100.Location = new Point(364, 395);
bt100.Text = "100";bt100Click += delegate
{
//这里是点击按钮执行的操作
}; Controls.Add(bt100);
for (int i = 0; i < 11; i++)
{
for (int j = 0; j < 11; j++)
{
bt[i * 10 + j] = new Button();
bt[i * 10 + j].Size = new Size(36, 29);
bt[i * 10 + j].Location = new Point((4 + 40 * (j - 1)), (80 + 35 * i));//每行每列的排序
bt[i * 10 + j].Text = (i * 10 + j).ToString();
Controls.Add(bt[i * 10 + j]); }
}
}