for (int i = 0; i < 8; i++) { Button a = new Button(); a.Width = 50; a.Text = "12" + i; a.Name = i.ToString(); a.BackColor = System.Drawing.Color.Red; a.Location = new System.Drawing.Point(20, i * 20); a.Click += new EventHandler(a_Click); this.Controls.Add(a); } } void a_Click(object sender, EventArgs e) { Button box = sender as Button; if (box.Name == "7") { Console.WriteLine("找到了"); } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } Label[] labels; private void Form2_Load(object sender, EventArgs e) { addLable(5); } void addLable(int n) { labels = new Label[n]; for (int i = 0; i < n; i++) { labels[i] = new Label(); labels[i].Text = "标签内容" + i; labels[i].Location = new Point(50,i*50); labels[i].Size = new Size(80,20); labels[i].Show(); this.Controls.Add(labels[i]); } }
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 8; i++)
{ Button a = new Button();
a.Width = 50;
a.Text = "12" + i;
a.Name = i.ToString();
a.BackColor = System.Drawing.Color.Red;
a.Location = new System.Drawing.Point(20, i * 20);
a.Click += new EventHandler(a_Click);
this.Controls.Add(a);
}
} void a_Click(object sender, EventArgs e)
{
Button box = sender as Button;
if (box.Name == "7")
{
Console.WriteLine("找到了");
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Label[] labels;
private void Form2_Load(object sender, EventArgs e)
{
addLable(5);
} void addLable(int n)
{
labels = new Label[n];
for (int i = 0; i < n; i++)
{
labels[i] = new Label();
labels[i].Text = "标签内容" + i;
labels[i].Location = new Point(50,i*50);
labels[i].Size = new Size(80,20);
labels[i].Show();
this.Controls.Add(labels[i]);
}
}
}
}这个是简易版 应该对你有帮助