111,098
社区成员




private Dictionary<string, string> eleKey = new Dictionary<string, string>() { { "H", "H property" }, { "Li", "Li property" }, { "Na", "Na property" }, { "Be", "Be property" }, { "C", "C property" } };
private void Form1_Load(object sender, EventArgs e)
{
int left=20, top=20;
int maxPerRow = 3; //每行最多button,超出换行
int col=1;
foreach(string key in eleKey.Keys)
{
Button btn = new Button();
btn.Size = new Size(40, 40);
btn.Location = new Point(left, top);
btn.Name = key;
btn.Text = key;
btn.Click += new EventHandler(showProperty);
this.Controls.Add(btn);
if (col >= maxPerRow)
{
left = 20;
top += 40;
col = 0;
}
else
{
left += 40;
}
col++;
}
}
private void showProperty(object sender,EventArgs e)
{
Button btn = (Button)sender;
//MessageBox.Show(btn.Name);
textBox1.Text = eleKey[btn.Name];
}
效果