Form class
Using System;
Using System.Collections.Generic;
Using System.Drawing;
Using System.IO;
Using System.Windows.Forms;
Using Tools;
Namespace Randomdraw
{
public partial class Randomform:form
{
configentity config;
bool Isstart = true;
List<string> NameList;
Private System.Windows.Forms.Timer Timer = new System.Windows.Forms.Timer ();
Random random = new random ();
int nameindex = 0;
Public Randomform ()
{
InitializeComponent ();
//config
config = loadconfig ();//Read the configuration file
if (file.exists (config. Backgroundfile)//Set background image
{
Image image = Image.FromFile (config. Backgroundfile);
this. BackgroundImage = image;
}; The
if (config. Fullscreen)//whether full screen
{
this. FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this. WindowState = System.Windows.Forms.FormWindowState.Maximized;
}//if
//Set text font
Lblname.backcolor = color.transparent;
Lblname.font = config. Font;
Lblname.forecolor = config. FontColor;
Loadlist ();//Read people list
Timer. Interval = config. The span;//timer is every tick interval
timer. Tick + = new EventHandler (timer_tick);//bind timer to call the processing method (sign) name (method)
}//form1
for each Tick
<summary>
Handling after pressing the keyboard
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Form1_keypress (object sender, KeyPressEventArgs e)
{
Switch (E.keychar)
{
Case (char) Keys.escape:
Case (char) Keys.enter:
Case (char) Keys.back:
Case (char) Keys.delete:
Case (char) Keys.end:
System.Environment.Exit (0);
Break
Case (char) Keys.space:
Working ();
Break
}//switch
}//form1_keypress
<summary>
After the mouse click Processing
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Randomform_mouseclick (object sender, MouseEventArgs e)
{
Working ();
}//randomform_mouseclick
<summary>
Read configuration
</summary>
Configentity Loadconfig ()
{
Configentity Configen = new configentity ();
if (file.exists ("Config.ini") ==false) {
MessageBox.Show ("Configuration file Config.ini not present, use default configuration.");
return Configen;
}//if
INI ini = new ini ("Config.ini");
String stragent;
Stragent=ini. ReadValue ("Remove_winner", "BASE");//Whether the winner is removed
if (stragent! = null && Stragent.trim (). Equals ("") = = False) {
if (Stragent.trim (). ToUpper (). Equals ("TRUE") = = False)
Configen.removewinner = false;
}//if
stragent = ini. ReadValue ("fullscreen", "BASE");
if (stragent! = null && Stragent.trim (). Equals ("") = = False)
{
if (Stragent.trim (). ToUpper (). Equals ("TRUE") = = False)
Configen.fullscreen = false;
}//if
stragent = ini. ReadValue ("Background_file", "BASE");
if (stragent! = null && Stragent.trim (). Equals ("") = = False)
{
Configen.backgroundfile = Stragent.trim ();
}//if
int span = 1;
stragent = ini. ReadValue ("SPAN", "BASE");
if (stragent! = null && Stragent.trim (). Equals ("") = = False)
{
BOOL isOk = Int. TryParse (Stragent.trim (), out span);
if (isOk = = False)
span = 1;
}//if
Configen.span = Span;
float fontsize=100;
stragent = ini. ReadValue ("Font_size", "BASE");
if (stragent! = null && Stragent.trim (). Equals ("") = = False)
{
bool IsOk = float. TryParse (Stragent.trim (), out fontSize);
if (isOk = = False)
fontSize = 100;
}//if
stragent = ini. ReadValue ("Font_color", "BASE");
if (stragent! = null && Stragent.trim (). Equals ("") = = False)
{
Configen.fontcolor= Color.fromname (Stragent.trim ());
}//if
String fontFamily = "Song Body";
stragent = ini. ReadValue ("font_family", "BASE");
if (stragent! = null && Stragent.trim (). Equals ("") = = False)
{
fontFamily = Stragent.trim ();
}//if
FontStyle fontstyle= fontstyle.regular;
stragent = ini. ReadValue ("Font_style", "BASE");
if (stragent! = null && Stragent.trim (). Equals ("") = = False)
{
Try
{
FontStyle = (FontStyle) enum.parse (typeof (FontStyle), stragent);
}
catch (Exception e) {
Console.WriteLine (E.tostring ());
FontStyle = Fontstyle.regular;
}//catch
}//if
Configen.font = new Font (fontFamily, fontSize, FontStyle);//font
return Configen;
}//loadconfig
<summary>
The timer handles every tick
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
void Timer_tick (object sender, EventArgs e)
{
if (namelist! = null && namelist.count > 0)
{
int next = random. Next (0, Namelist.count);
Lblname.text = Namelist[next];
Nameindex = Next;
}//if
}//timer_tick
<summary>
Run (pause or start)
</summary>
void working ()
{
if (Isstart)
Start ();
Else
Stop ();
}//working
<summary>
Begin
</summary>
void Start ()
{
Isstart = false;
Timer. Start ();
}//start
<summary>
Stop it
</summary>
void Stop ()
{
Isstart = true;
Timer_tick (null, NULL);//pause once more randomly
Timer. Stop ();
if (config. Removewinner)
Removewinner ();
}//stop
<summary>
Remove winners
</summary>
void Removewinner ()
{
if (namelist! = null && namelist.count > 0)
{
Try
{
Namelist.removeat (Nameindex);
}
catch (Exception e)
{
Console.WriteLine (E.tostring ());
MessageBox.Show ("The data in the Remove list array whose index is" +nameindex+ "failed.");
}//catch
}//if
}//removewinner
<summary>
Read the list of people
</summary>
void Loadlist ()
{
if (file.exists ("list.txt") = = False)
{
Return
}//if
Try
{
NameList = null;
String[] names;
names = System.IO.File.ReadAllLines ("List.txt");
Lblname.text = "Ready";
NameList = new list<string> ();
foreach (String name in names)
{
Namelist.add (name. Replace ("", ""));
}//foreach
}
catch (Exception e)
{
Lblname.text = "Read list failed:" + e.message;
}//
}//loadlist
}//form1
}
Configentity class
Using System;
Using System.Collections.Generic;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Randomdraw
{
Class Configentity
{
private string backgroundfile= "backgroud.jpg";
private bool Removewinner=true;
private bool Fullscreen=true;
Private System.Drawing.Font Font = new System.Drawing.Font ("Arial", 100);
private string readyword= "ready";
private int span=1;
Private Color fontcolor = Color.Black;
public string Backgroundfile
{
Get
{
return backgroundfile;
}
Set
{
Backgroundfile = value;
}
}
public bool Removewinner
{
Get
{
return removewinner;
}
Set
{
Removewinner = value;
}
}
public bool Fullscreen
{
Get
{
return fullscreen;
}
Set
{
fullscreen = value;
}
}
Public font Font
{
Get
{
return font;
}
Set
{
Font = value;
}
}
public string Readyword
{
Get
{
return Readyword;
}
Set
{
Readyword = value;
}
}
public int Span
{
Get
{
return span;
}
Set
{
span = value;
}
}
Public Color FontColor
{
Get
{
return fontcolor;
}
Set
{
FontColor = value;
}
}
}//configentity
}//randomdraw
INI file Operation class: (Reference reference: http://www.cnblogs.com/uu102/p/4083501.html)
Ini.cs
Random Draw Software