using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Threading;namespace 摇奖机{ public partial class Form1 : Form { public Form1() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false;//关闭跨线程检测,底下是死循环,两个线程会抛异常 } bool flag = false; string str = string.Empty; private void pictureBox1_Click(object sender, EventArgs e) { if (Convert.ToInt16(pictureBox1.Tag) == 0)//判断开关状态,为0则打开 { pictureBox1.Image = Properties.Resources.switch_on_normal; pictureBox1.Tag = 1; flag = true; Thread th = new Thread(start);//新建线程,用于运行方法start th.IsBackground = true; th.Start();//开始线程 } else { pictureBox1.Image = Properties.Resources.switch_off_clicked;//关闭开关 pictureBox1.Tag = 0; flag = false; if (label3.Text == label2.Text && label2.Text == label1.Text)//下为中奖号码判断 { textBox1.Text = string.Format("恭喜你中到一等奖{0},么么哒,努力写代码噢", str); } else if (label3.Text == label2.Text || label2.Text == label1.Text || label1.Text == label3.Text) { textBox1.Text = string.Format("恭喜你中到二等奖{0},再接再厉", str); } else { textBox1.Text = string.Format("很抱歉,你没有中奖{0},换个抽奖姿势把", str); } } } private void start() { Random r = new Random();//生成3个随机数 while (flag) { label3.Text = r.Next(0, 10).ToString(); Thread.Sleep(100);//数字变化太快,所以挂起0.1s线程; label2.Text = r.Next(0, 10).ToString(); Thread.Sleep(100); label1.Text = r.Next(0, 10).ToString(); Thread.Sleep(100);} } private void button1_Click(object sender, EventArgs e) { textBox1.Clear();//清空文本 } private void groupBox1_Enter(object sender, EventArgs e) { } private void timer1_Tick(object sender, EventArgs e) { label6.Text = label6.Text.Substring(1) label6.Text.Substring(0, 1);//提示信息的播报 } }}
评论