C#摇奖程序
private void Form1_Load(object sender, EventArgs e) { //取消跨线层访问控件的判断 Control.CheckForIllegalCrossThreadCalls = false; } //声明一外bool变量用于控制随机数方法 bool bl = false; private void button1_Click(object sender, EventArgs e) { //判读方是否在进运行 if (bl == false) { bl = true; button1.Text = "停止"; //做一个新线层,让他去做随机数的方法 Thread trd = new Thread(PlayGame); trd.Start(); } else { button1.Text = "开始"; bl = false; } } //做一个随机数的方法 private void PlayGame() { Random rm = new Random(); while (bl) { //将随机数赋值给三个label控件 label1.Text =rm.Next(1, 10).ToString(); label2.Text = rm.Next(1, 10).ToString(); label3.Text = rm.Next(1, 10).ToString(); } if (label1.Text == label2.Text || label1.Text == label3.Text || label2.Text == label3.Text) { MessageBox.Show("中奖了"); } else { MessageBox.Show("很可惜,没中奖"); } }