运行效果
相关操作
1.添加抽奖文本
创建如图所示txt文本,并以Unicode编码保存在当前项目的\bin\Debug文件夹中
2.添加控件
添加按钮,label以及timer控件。
代码如下
由于需要对文件进行操作需添加以下代码
using System.IO;
在全局中添加以下代码,读取文件信息
static string path = "抽奖.txt";
string[] content = File.ReadAllLines(path);
为button按钮添加以下代码
if (button1.Text == "开始")
{ button1.Text = "结束"; timer1.Start(); }
else
{ button1.Text = "开始"; timer1.Stop(); }
为timer添加以下代码
Random r = new Random();
int i = r.Next(0, 5);
label1.Text = content[i];
为Form添加以下代码
timer1.Start();
如果不希望添加timer控件的开始和结束代码,可在timer的属性的Enabled设置为True