namespace 抽奖
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random ran = new Random();
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
try
{
for (int i = 0; i < Convert.ToInt32(textBox1.Text); i++)//····textbox1是显示button数
{
Button btn = new Button();
btn.Name = i.ToString();
btn.Text = ran.Next(Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox3.Text)).ToString();//·····textbox2和textbox3是抽奖数范围
btn.Location = new Point(8 + i * 80, 8);
panel1.Controls.Add(btn);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
}
没有考虑按钮到容器的边界问题,我也是刚刚学习C#没几天,希望能帮到你
// ......
//界面拖一个TextBox(id=“TXT1”)和Button(id=“BTN1”)
//BTN1的Click事件中
{
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Name != "BTN1")
{
this.Controls.Remove(c);
}
}
}
Random random = new Random();
int Nums = 0;
try
{
Nums = int.Parse(TXT1.Text);
}
catch
{
MessageBox.Show("您为输入数字或输入的不是数字形式!", "提示");
}
for (int i = 0; i < Nums; i++)
{
Button btn = new Button();
btn.Text = (random.Next(5000) + 1).ToString();
btn.Top = 30 + (i / 5) * 30;
btn.Left = (100 * (i % 5));
this.Controls.Add(btn);
}
}
五个button,五个random