private void Form1_Load(object sender, EventArgs e)
{
Random ra = new Random();
int x, y;
for (int i = 0; i < 10; i++)
{
x = ra.Next(300);
y = ra.Next(300);
Button btn=new Button();
btn.Location = new Point(x,y);
btn.Size = new Size(40, 20);
btn.Text = "雷";
btn.Parent = this;
}
}
using System;
using System.Windows.Forms;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
int width = Width;
int height = Height;
Control[] ctrl = new Control[10];
Button btn = null;
Random ro = new Random();
for (int i = 0; i < 10; i++)
{
btn = new Button();
btn.Width = 20;
btn.Height = 20;
btn.Text = i.ToString();
btn.Left = ro.Next(0, width - btn.Width);
btn.Top = ro.Next(0, height - btn.Height);
ctrl[i] = btn;
}
Controls.AddRange(ctrl);
}
}
}
有问题问~
Random a = new Random();
button1.Location = new Point(a.Next(0, 100), a.Next(0, 100));