隨機點名程序方便好用

 

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Windows.Forms;  
  5. using System.Drawing;  
  6.  
  7. namespace Randoms  
  8. {  
  9.     public partial class Main : Form  
  10.     {  
  11.         FileStream fs = null;  
  12.         StreamReader sr = null;  
  13.         //StreamWriter sw = null;  
  14.         List<string> names = new List<string>();  
  15.         Random rd = new Random();  
  16.         int jg = 0;  
  17.         //string syspath = System.Environment.CommandLine;//當前程序路徑  
  18.         string syspath = System.Windows.Forms.Application.StartupPath;  
  19.  
  20.         public Main()  
  21.         {  
  22.             InitializeComponent();  
  23.  
  24.             //GetData();//獲取數據  
  25.               
  26.                  
  27.  
  28.         }  
  29.  
  30.         private void GetData()  
  31.         {  
  32.             string Namesaddress = syspath + "\\Names.txt";  
  33.             try 
  34.             {  
  35.  
  36.                 fs = new FileStream(Namesaddress, FileMode.Open);  
  37.  
  38.                 sr = new StreamReader(fs);  
  39.  
  40.                 //string count=sr.ReadLine();  
  41.  
  42.                 //while (count!=null)   
  43.                 //{  
  44.                 //    name.Add(count.ToString());  
  45.                 //}  
  46.  
  47.  
  48.                 while (true)  
  49.                 {  
  50.                     string s = sr.ReadLine();  
  51.                     if (s != null)  
  52.                         names.Add(s);  
  53.                     else 
  54.                         break;  
  55.                 }  
  56.  
  57.             }  
  58.             catch (Exception)  
  59.             {  
  60.  
  61.                 throw;  
  62.             }  
  63.             finally 
  64.             {  
  65.                 sr.Close();  
  66.                 fs.Close();  
  67.             }  
  68.         }  
  69.  
  70.         /// <summary>  
  71.         /// 開始點名  
  72.         /// </summary>  
  73.         /// <param name="sender"></param>  
  74.         /// <param name="e"></param>  
  75.         private void button1_Click(object sender, EventArgs e)  
  76.         {  
  77.             this.button2.Enabled = true;  
  78.             lab.ForeColor = Color.Black;  
  79.             if (names.Count == 0)  
  80.             {  
  81.  
  82.                 this.time.Enabled = false;  
  83.                 lab.Text = "Please Reload At First";  
  84.  
  85.  
  86.             }  
  87.             else 
  88.             {  
  89.                 this.time.Enabled = true;  
  90.             }  
  91.  
  92.         }  
  93.         /// <summary>  
  94.         /// 計數器  
  95.         /// </summary>  
  96.         /// <param name="sender"></param>  
  97.         /// <param name="e"></param>  
  98.         private void time_Tick(object sender, EventArgs e)  
  99.         {  
  100.               
  101.                 int rf = names.Count;  
  102.                 jg = rd.Next(0, rf);  
  103.                 lab.Text = names[jg];  
  104.               
  105.              
  106.               
  107.         }  
  108.         /// <summary>  
  109.         /// 停止  
  110.         /// </summary>  
  111.         /// <param name="sender"></param>  
  112.         /// <param name="e"></param>  
  113.         private void button2_Click(object sender, EventArgs e)  
  114.         {  
  115.  
  116.             if (names.Count == 0)  
  117.             {  
  118.                 this.time.Enabled = false;  
  119.                 lab.Text = "The End";  
  120.                 //GetData();  
  121.             }  
  122.             else 
  123.             {  
  124.                 lab.ForeColor = Color.Blue;  
  125.                 this.time.Enabled = false;  
  126.                 names.RemoveAt(jg);  
  127.             }  
  128.                   
  129.               
  130.         }  
  131.         /// <summary>  
  132.         /// 加載所有人  
  133.         /// </summary>  
  134.         /// <param name="sender"></param>  
  135.         /// <param name="e"></param>  
  136.         private void Main_Load(object sender, EventArgs e)  
  137.         {  
  138.             string Namesaddress = syspath + "\\Names.txt";  
  139.             if (System.IO.File.Exists(Namesaddress) )  
  140.             {  
  141.                 GetData();  
  142.             }  
  143.             else 
  144.             {  
  145.  
  146.                 MessageBox.Show("查看Names.txt文件是否存在");  
  147.                 Application.Exit();   
  148.             }  
  149.         }  
  150.  
  151.         /// <summary>  
  152.         /// 從新加載  
  153.         /// </summary>  
  154.         /// <param name="sender"></param>  
  155.         /// <param name="e"></param>  
  156.         private void button3_Click(object sender, EventArgs e)  
  157.         {  
  158.             lab.ForeColor = Color.Black;  
  159.             this.time.Enabled = false;  
  160.             lab.Text = "Load Complete";  
  161.             GetData();  
  162.             this.button2.Enabled = false;  
  163.         }  
  164.     }  
  165. }  

要程序的請留言~!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章