双色球生成器
public partial class Form1 : Form { private bool flag = true; delegate void SetTextDelegate(Label label, string text); private Label[] labels; private Font font; public Form1() { InitializeComponent(); } /// <summary> /// 制造号码 /// </summary> private int[] MakeNums() { int[] arr = new int[7]; Random rand = new Random(); Hashtable ht = new Hashtable(); //产生不重复的随机数 int key = 0; while (ht.Count <= 6) { int val = rand.Next(1, 34); if (!ht.ContainsValue(val)) { ht.Add(key, val); key++; } } //得到随机数 for (int i = 0; i < 6; i++) { arr[i] = (int)ht[i]; } //冒泡排序 int temp; for (int outer = 5; outer >= 0; outer--) { for (int inner = 0; inner <= outer - 1; inner++) { if (arr[inner] > arr[inner + 1]) { temp = arr[inner]; arr[inner] = arr[inner + 1]; arr[inner + 1] = temp; } } } //制造蓝色球 arr[6] = rand.Next(1, 17); return arr; } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "start") { flag = true; button1.Text = "stop"; ThreadStart TStart = new ThreadStart(Start); Thread thread = new Thread(TStart); thread.Start(); } else { flag = false; button1.Text = "start"; } } private void Start() { while (flag) { PrintNum(); Thread.Sleep(88); } } private void PrintNum() { int[] arr = MakeNums(); for (int i = 0; i < 7; i++) { Invoke( new MethodInvoker(delegate() { SetText(labels[i], arr[i].ToString()); } )); } //for (int i = 0; i < 7; i++) //{ // SetTextDelegate mydelegate = new SetTextDelegate(SetText); // try // { // Invoke(mydelegate, labels[i], arr[i].ToString()); // } // catch { return; } //} } private void SetText(Label label, string text) { label.Text = text; label.TextAlign = ContentAlignment.MiddleCenter; label.Font = font; if (label.Name == "label7") { label.ForeColor = Color.Blue; } else { label.ForeColor = Color.Red; } } private void Form1_Load(object sender, EventArgs e) { labels = new Label[] { label1, label2, label3, label4, label5, label6, label7 }; font = new Font("Arial Black", 36); } }