|
发表于 2020-5-13 21:00:01
|
显示全部楼层
利用timer就很简单了,把你要抽奖的号码都放到 numbers 数组里。
private string[] numbers = null;
private void btnStart_Click(object sender, System.EventArgs e)
{
this.timer1.Interval = 100;
this.timer1.Start();
this.btnStart.Enabled = false;
this.btnStop.Enabled = true;
}
private void Form1_Load(object sender, System.EventArgs e)
{
numbers = new string[] {"1","2","3","4","5","6","7","8","9","0"};
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.btnStart.Enabled = true;
this.btnStop.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.txtNumber.Text = GetRandomNumber(this.numbers);
}
private string GetRandomNumber(string[] numbers)
{
if(numbers == null || numbers.Length == 0)
return "";
Random rdm = new Random(unchecked((int)DateTime.Now.Ticks));
int index = rdm.Next(0, numbers.Length-1);
return numbers[index];
}
private void btnStop_Click(object sender, System.EventArgs e)
{
this.timer1.Stop();
this.btnStart.Enabled = true;
this.btnStop.Enabled = false;
} |
|