处理求一个有源码的C#编写的随机点名(记录已经点过名字的人)
我这有个随机抽奖的,一样的,代码贴给你,最主要的用个timer控件就可以了
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;
using System.Collections;//使用arraylist数组所添加的引用
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public static int index;
ArrayList numbers = new ArrayList();//实例化数组对象
public Form1()
{
InitializeComponent();
numbers.Add("20102831");
numbers.Add("20102832");
numbers.Add("20102833");
numbers.Add("20102834");
numbers.Add("20102835");
numbers.Add("20102836");
numbers.Add("20102837");
numbers.Add("20102838");
numbers.Add("20102839");
numbers.Add("20102840");
numbers.Add("20102841");
//向数组中添加数据
}
//点击开始按钮触发事件
private void btnStart_Click(object sender, EventArgs e)
{
if (numbers.Count == 0)//当初始数组长度为空时,摇奖结束!
{
MessageBox.Show("摇奖结束");
}
else
{
timer1.Enabled = true;//启动timer控件
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Random ran = new Random();//定义一个随机数发生器实例
index = ran.Next(numbers.Count);//在0到numbers.count(即arraylist数组长度)之间随机取数!
object RandKey = numbers[index];//获得数组下标为index的数组元素
lblPrazeNum.Text = RandKey.ToString(); //将这个值赋给label控件即 lblPrazeNum
}
//点击停止按钮时触发事件
private void btnStop_Click(object sender, EventArgs e)
{
if (numbers.Count == 0)
{
MessageBox.Show("摇奖结束!");
}
else
{
timer1.Enabled = false;
lblPrizeList.Text += lblPrazeNum.Text + "\n";
numbers.RemoveAt(index);
if (numbers.Count == 0)
{
MessageBox.Show("摇奖结束!");
}
}
}
//点击清空按钮触发事件
private void button1_Click(object sender, EventArgs e)
{
lblPrizeList.Text = "";
}
}
}