那个程序未免也太傻瓜式了,一点就产生了一个数,毫无悬念的就出来结果了。
就想让那个过程动起来,可以人为的控制开始和结束的过程。
也就是一个不断给lalble赋值的过程,于是就写了一个while的死循环,不点暂停,它就一直在给lalble赋值。
那么问题来了,這样不断赋值的过程就造成的窗体的假死现象。程序就死在那,暂停也没有用。
于是找了点资料。开启一个新的线程来执行这个命令就好了。
上图(觉得界面效果做得还不错)
代码其实也蛮简单的(一个选姓名的,一个选彩票的,可以自己设置候选项)
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using CCWin;
10 using System.Threading;
11 using System.Media;
12 using System.IO;
13
14 namespace 抽奖机
15 {
16 public partial class Form1 : CCSkinMain //Form//CCSkinMain
17 {
18 public Form1()
19 {
20 InitializeComponent();
21 }
22
23 Random R = new Random();//随机数
24
25 //加载
26 private void Form1_Load(object sender, EventArgs e)
27 {
28 CheckForIllegalCrossThreadCalls = false;//禁止跨线程检查
29 this.TabPage.Parent = this.TabControl1;//显示page1
30 }
31
32 //按姓名随机选
33 bool a = false;
34 private void btnNameGo_Click(object sender, EventArgs e)
35 {
36 //music.SoundLocation = @"1.wav"; //路径,背景音乐
37 Thread th2 = new Thread(name);//创建一个线程(来调用name方法)
38 if (a == false)
39 {
40 th2.IsBackground = true;//设置为后台线程
41 th2.Start();//开启线程
42 btnNameGo.Text = "停 止";
43 //music.Play();//播放背景音乐
44 a = true;
45 }
46 else
47 {
48 a = false;
49 th2.Abort();//结束线程
50 btnNameGo.Text = "开 始";
51 //music.Stop();//停止播放
52 }
53 }
54 public void name()
55 {
56 while(a)
57 {
58 int x=R.Next(0, lbl.Items.Count);
59 lalN.Text = lbl.Items[x].ToString();
60 }
61 }
62
63 //福彩3D
64 bool b = false;
65 private void btnGo_Click_1(object sender, EventArgs e)
66 {
67 Thread th = new Thread(PlayGame);
68 if (b == false)
69 {
70 th.IsBackground = true;
71 btnGo.Text = "停 止";
72 th.Start();
73 b = true;
74 }
75 else
76 {
77 b = false;
78 th.Abort();
79 btnGo.Text = "开 始";
80 }
81 }
82 public void PlayGame()
83 {
84 while (b)
85 {
86 lalNum1.Text = R.Next(0, 10).ToString();
87 lalNum2.Text = R.Next(0, 10).ToString();
88 lalNum3.Text = R.Next(0, 10).ToString();
89 }
90 }
91
92 //背景音乐,去掉了
93 //SoundPlayer music = new SoundPlayer();
94
95
96 //添加学生姓名
97 private void btnTj_Click(object sender, EventArgs e)
98 {
99 if (txtName.Text=="")
100 {
101 MessageBox.Show("添加的姓名不能为空!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
102 return;
103 }
104 lbl.Items.Add(txtName.Text);
105 //MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
106 }
107
108 //清空所有名单
109 private void btnQK_Click(object sender, EventArgs e)
110 {
111 if (MessageBox.Show("您确定要移除所有名单吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk)==DialogResult.Yes)
112 {
113 lbl.Items.Clear();
114 }
115 }
116 int i = -1;
117 private void lbl_SelectedIndexChanged(object sender, EventArgs e)
118 {
119 txtName.Text = lbl.Text;
120 i = lbl.SelectedIndex;
121 }
122
123 //移除一个人
124 private void btnCL_Click(object sender, EventArgs e)
125 {
126 if (i == -1)
127 {
128 MessageBox.Show("请先选中移除目标!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
129 return;
130 }
131 lbl.Items.RemoveAt(i);
132 i = -1;
133 }
134
135 //图片的
136 bool c = false;
137 private void btnIMG_Click(object sender, EventArgs e)
138 {
139 Thread th3 = new Thread(PlayImg);
140 if (c == false)
141 {
142 th3.IsBackground = true;
143 btnIMG.Text = "停 止";
144 th3.Start();
145 c = true;
146 }
147 else
148 {
149 c = false;
150 th3.Abort();
151 btnIMG.Text = "开 始";
152 }
153 }
154 public void PlayImg()
155 {
156 int max = imageList1.Images.Count;
157 while (c)
158 {
159 int x1 = R.Next(0, lbl.Items.Count);
160 lalName1.Text = lbl.Items[x1].ToString();//图片的
161 int x2 = R.Next(0, lbl.Items.Count);
162 lalName2.Text = lbl.Items[x2].ToString();
163 }
164 }
165 }
166 }