- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Windows.Forms;
- using System.Drawing;
- namespace Randoms
- {
- public partial class Main : Form
- {
- FileStream fs = null;
- StreamReader sr = null;
- //StreamWriter sw = null;
- List<string> names = new List<string>();
- Random rd = new Random();
- int jg = 0;
- //string syspath = System.Environment.CommandLine;//當前程序路徑
- string syspath = System.Windows.Forms.Application.StartupPath;
- public Main()
- {
- InitializeComponent();
- //GetData();//獲取數據
- }
- private void GetData()
- {
- string Namesaddress = syspath + "\\Names.txt";
- try
- {
- fs = new FileStream(Namesaddress, FileMode.Open);
- sr = new StreamReader(fs);
- //string count=sr.ReadLine();
- //while (count!=null)
- //{
- // name.Add(count.ToString());
- //}
- while (true)
- {
- string s = sr.ReadLine();
- if (s != null)
- names.Add(s);
- else
- break;
- }
- }
- catch (Exception)
- {
- throw;
- }
- finally
- {
- sr.Close();
- fs.Close();
- }
- }
- /// <summary>
- /// 開始點名
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button1_Click(object sender, EventArgs e)
- {
- this.button2.Enabled = true;
- lab.ForeColor = Color.Black;
- if (names.Count == 0)
- {
- this.time.Enabled = false;
- lab.Text = "Please Reload At First";
- }
- else
- {
- this.time.Enabled = true;
- }
- }
- /// <summary>
- /// 計數器
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void time_Tick(object sender, EventArgs e)
- {
- int rf = names.Count;
- jg = rd.Next(0, rf);
- lab.Text = names[jg];
- }
- /// <summary>
- /// 停止
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button2_Click(object sender, EventArgs e)
- {
- if (names.Count == 0)
- {
- this.time.Enabled = false;
- lab.Text = "The End";
- //GetData();
- }
- else
- {
- lab.ForeColor = Color.Blue;
- this.time.Enabled = false;
- names.RemoveAt(jg);
- }
- }
- /// <summary>
- /// 加載所有人
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Main_Load(object sender, EventArgs e)
- {
- string Namesaddress = syspath + "\\Names.txt";
- if (System.IO.File.Exists(Namesaddress) )
- {
- GetData();
- }
- else
- {
- MessageBox.Show("查看Names.txt文件是否存在");
- Application.Exit();
- }
- }
- /// <summary>
- /// 從新加載
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button3_Click(object sender, EventArgs e)
- {
- lab.ForeColor = Color.Black;
- this.time.Enabled = false;
- lab.Text = "Load Complete";
- GetData();
- this.button2.Enabled = false;
- }
- }
- }
要程序的請留言~!