using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int num = 1; //声明变量
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Button btn = new Button(); //动态创新按钮,创建实例
btn.Text = num.ToString(); //将num的值赋值给按钮的text属性,num自增
btn.Width = 24; //设置控件的长度
btn.Height = 20; //设置控件的宽度
btn.Top = i * 20 + 3; //设置控件距离容器顶部的位置
btn.Left = j * 24 + 5; //设置控件距离容器左边的位置
Controls.Add(btn); //将控件显示在容器中
btn.Click+=new EventHandler(btn_Click);
num++;
}
}
Label lab = new Label();
lab.Top = 68;
lab.Left = 29;
lab.Name = "LableTxt";
Controls.Add(lab);
}
private void btn_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
Label newlable = (Label)Controls.Find("LableTxt", true)[0];
newlable.Text = "你单击了" + b.Text + "号按钮";
b.Text = "*";
}
}
}