这是我的代码:
Random rnd = new Random();
int x, y,n,c= 0;
private void beginnerToolStripMenuItem_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
List<int> mynumber = new List<int>() {0,1,2,3,4,5,6,7,8,9};
List<Color> mycolor = new List<Color>() { Color.AliceBlue, Color.AntiqueWhite, Color.Aqua, Color.Aquamarine, Color.Azure, Color.Beige, Color.Blue, Color.Brown, Color.Chocolate, Color.DarkOrange, Color.Red,Color.Red,Color.Yellow,Color.SpringGreen };
for (int i = 0; i < 5; i++)
{
Button btn = new Button();
btn.Size = new Size(50, 50);
for (int j = 0; j < 5; j++)
{
x = rnd.Next(0, 500);
y = rnd.Next(0, 430);
n = rnd.Next(0, 10);
c = rnd.Next(0, mycolor.Count);
btn.Location = new Point(x, y);
btn.Text = mynumber[n].ToString();
btn.BackColor = Color.Black;
btn.Font = new Font("Tahoma", 12,FontStyle.Bold);
btn.ForeColor = mycolor[c];
buttons.Add(btn);
pnlbtn.Controls.Add(btn);
}
}