逻辑层代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace AskQuestionInClass
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string filename = null;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//如果选择文件后,点击OK按钮则将选区文件赋值给FileDialog
filename = openFileDialog1.FileName;
//openFileDialog1.FileName;FileName是包含了文件路劲和文件名字的一个字符串
}
else
{
MessageBox.Show("你没有选择文件!是否放弃选择?");
return;
}
}
//集合 list<>;dictiongary<,>或者数组;
List<string> studentlist;//用来储存学生名单,提供名单自动抽取时的载体
string[] newstulist;//同来与文本文件名单对接,用来读取数据
private void button2_Click(object sender, EventArgs e)
{
if (filename == "")
{
MessageBox.Show("没有选择班级名单或者名单为空");
return;
}
else
{
studentlist = System.IO.File.ReadAllLines(filename).ToList();
//将文本文件的学生名单读取出来,存在list集合中
if(button2.Text=="开始")
{
timer1.Start();
button2.Text = "结束";
}
else
{
timer1.Stop();
button2.Text = "开始";
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Random rd1 = new Random();
int temp = rd1.Next(0, studentlist.Count);//随即确定被读取对象
label2.Text = studentlist[temp].ToString();//读值对象的值
label2.ForeColor = Color.FromArgb(rd1.Next(0, 200 * 200));//变色
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
timer1.Interval=trackBar1.Value;
}
}
}
支持TXT格式,其他格式会出现乱码
创建TXT文本
写几行文字就可以
运行窗体
选择你创建的TXT文本点击开始
点击结束随机选中