我上次做的软件的作用就是实现《怎样学习WinForm》的那个例题,它有两个部分,一个是客户端,它的代码如下:
View Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace Assigna { public partial class Assigna : Form { public Assigna() { InitializeComponent(); } public int num = 0; //Display various situation public int time = 1; //Timer1 record of changes public int counter = 0; //Digital changes public string state; bool[] bn = { true, true, false, false, false, false }; //An initial value private void Form1_Load(object sender, EventArgs e) { timer2.Enabled = true; //Timer2 is keep receiving if (!File.Exists(@"d://a.text")) { btnClose.Enabled = bn[0]; btnStart.Enabled = bn[1]; btnStop.Enabled = bn[2]; btnReset.Enabled = bn[3]; timer1.Enabled = bn[4]; //Initial value } else { StreamReader sr = new StreamReader("d://a.text"); counter = Convert.ToInt32(sr.ReadLine()); textBox2.Text = sr.ReadLine(); btnClose.Enabled = Convert.ToBoolean(sr.ReadLine()); btnStart.Enabled = Convert.ToBoolean(sr.ReadLine()); btnStop.Enabled = Convert.ToBoolean(sr.ReadLine()); btnReset.Enabled = Convert.ToBoolean(sr.ReadLine()); timer1.Enabled = Convert.ToBoolean(sr.ReadLine()); sr.Close(); textBox1.Text = Convert.ToString(counter); //Historical value } } public void btnClose_Click(object sender, EventArgs e) { state = textBox2.Text; bn[0] = btnClose.Enabled; bn[1] = btnStart.Enabled; bn[2] = btnStop.Enabled; bn[3] = btnReset.Enabled; bn[4] = timer1.Enabled; StreamWriter sw = new StreamWriter("d://a.text"); sw.WriteLine(counter); sw.WriteLine(state); for (int i = 0; i < 5; i++) { sw.WriteLine(bn[i]); } sw.Close(); //Keep records StreamWriter aw = new StreamWriter("d://c.text"); aw.WriteLine("0"); aw.Close(); //Send close information for (int i = 0; i < 10; i++) { File.Delete(@"d://d.text"); //Deleting files, prevent the restart, anomalies } Close(); } private void btnStart_Click(object sender, EventArgs e) { btnStart.Enabled = false; btnStop.Enabled = true; btnReset.Enabled = true; btnClose.Enabled = true; timer1.Enabled = true; if (time == 1) { textBox2.Text = "INC & DEC :Increasing "; StreamWriter aw = new StreamWriter("d://c.text"); aw.WriteLine("1"); aw.Close(); } else if(time == 2) { textBox2.Text = "INC & DEC : Decreasing "; StreamWriter aw = new StreamWriter("d://c.text"); aw.WriteLine("6"); aw.Close(); } //Send start information } private void btnStop_Click(object sender, EventArgs e) { btnStop.Enabled = false; if (timer1.Enabled == true && time == 1) { btnStart.Enabled = true; textBox2.Text = "INC & DEC :Stop Increasing "; timer1.Enabled = false; StreamWriter aw = new StreamWriter("d://c.text"); aw.WriteLine("3"); aw.Close(); } else if (timer1.Enabled ==false) { textBox2.Text = "INC :Stop Decreasing "; StreamWriter aw = new StreamWriter("d://c.text"); aw.WriteLine("4"); aw.Close(); } else if(timer1.Enabled == true && time == 2 ) { textBox2.Text = "INC & DEC :Stop Decreasing "; btnStart.Enabled = true; timer1.Enabled = false; StreamWriter aw = new StreamWriter("d://c.text"); aw.WriteLine("5"); aw.Close(); } //Send stop information } private void btnReset_Click(object sender, EventArgs e) { num = 0; counter = 0; textBox1.Text = "0"; textBox2.Text = "Initial state "; btnClose.Enabled = true; btnStart.Enabled = true; btnStop.Enabled = false; btnReset.Enabled = false; timer1.Enabled = false; time = 1; //reset StreamWriter aw = new StreamWriter("d://c.text"); aw.WriteLine("7"); aw.Close(); } //Send the information reset private void timer1_Tick(object sender, EventArgs e) { if(time == 1) { timer1.Interval = 1500; counter++; textBox1.Text = Convert.ToString(counter); if(counter > 9) { timer1.Enabled = false; textBox2.Text = "INC : Increasing "; StreamWriter aw = new StreamWriter("d://c.text"); aw.WriteLine("2"); aw.Close(); } //In timer1 digital growth } else if(time == 2) { timer1.Interval = 2000; counter--; textBox1.Text = Convert.ToString(counter); while (counter < 0) { StreamWriter aw = new StreamWriter("d://c.text"); aw.WriteLine("8"); aw.Close(); Close(); } //In timer1 digital reduce } } private void timer2_Tick(object sender, EventArgs e) { if(File.Exists(@"d://d.text")) { //Whether have the information the file using (StreamReader ar = new StreamReader("d://d.text")) { string line; if((line =ar.ReadLine()) != null) { num = Convert.ToInt32(line); ar.Close(); //Read in information } } switch (num) //According to information change { case 0: btnClose_Click(sender, e); Close(); break; case 1: textBox2.Text = "INC & DEC: Decreasing"; timer1.Enabled = true; time = 2; break; case 2: timer1.Enabled = false; btnStart.Enabled = true; btnStop.Enabled = false; textBox2.Text = "INC & DEC: Stop Increasing "; break; case 3: btnStop.Enabled = false; textBox2.Text = "INC :Stop Increasing "; break; case 4: btnStop.Enabled = true; textBox2.Text = "INC :Increasing "; break; case 5: btnStop.Enabled = false; btnStart.Enabled = true; timer1.Enabled = false; textBox2.Text = "INC & DEC:Stop Decreasing "; break; case 6: num = 0; counter = 0; textBox1.Text = "0"; textBox2.Text = "Initial state "; btnClose.Enabled = true; btnStart.Enabled = true; btnStop.Enabled = false; btnReset.Enabled = false; timer1.Enabled = false; time = 1; break; default: break; } File.Delete(@"d://d.text"); // Delete information file } } } }
第二个是服务器端,代码如下:
View Code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace Assignb { public partial class Assignb : Form { public Assignb() { InitializeComponent(); } public int num = 0; //Display various situation public int counter = 0; //Digital changes public string state = "Initial State "; bool[] bc = { true, false, false, false, false }; //An initial value private void Form1_Load(object sender, EventArgs e) { timer2.Enabled = true; //Timer2 is keep receiving if (!File.Exists(@"d://b.text")) { btnClose.Enabled = bc[0]; btnStart.Enabled = bc[1]; btnStop.Enabled = bc[2]; btnReset.Enabled = bc[3]; timer1.Enabled = bc[4]; //Initial value } else { StreamReader sr = new StreamReader("d://b.text"); counter = Convert.ToInt32(sr.ReadLine()); textBox2.Text = sr.ReadLine(); btnClose.Enabled = Convert.ToBoolean(sr.ReadLine()); btnStart.Enabled = Convert.ToBoolean(sr.ReadLine()); btnStop.Enabled = Convert.ToBoolean(sr.ReadLine()); btnReset.Enabled = Convert.ToBoolean(sr.ReadLine()); timer1.Enabled = Convert.ToBoolean(sr.ReadLine()); textBox1.Text = Convert.ToString(counter); sr.Close(); //Historical value } } public void btnClose_Click(object sender, EventArgs e) { bc[0] = btnClose.Enabled; bc[1] = btnStart.Enabled; bc[2] = btnStop.Enabled; bc[3] = btnReset.Enabled; bc[4] = timer1.Enabled; state = textBox2.Text; StreamWriter sw = new StreamWriter("d://b.text"); sw.WriteLine(counter); sw.WriteLine(state); for (int i = 0; i < 5; i++) { sw.WriteLine(bc[i]); } sw.Close(); //Keep records StreamWriter bw = new StreamWriter("d://d.text"); bw.WriteLine("0"); bw.Close(); //Send close information for (int i = 0; i < 10; i++ ) { File.Delete(@"d://d.text"); } //Deleting files, prevent the restart, anomalies Close(); } private void btnStart_Click(object sender, EventArgs e) { timer1.Enabled = true; btnStart.Enabled = false; btnStop.Enabled = true; btnReset.Enabled = true; btnClose.Enabled = true; if(counter != 0 && counter != 10) { textBox2.Text = "INC :Increasing"; StreamWriter bw = new StreamWriter("d://d.text"); bw.WriteLine("4"); bw.Close(); } } //Send start information private void btnStop_Click(object sender, EventArgs e) { btnStop.Enabled = false; if(timer1.Enabled == false && counter == 0) { textBox2.Text = "INC & DEC: Stop Increasing "; StreamWriter bw = new StreamWriter("d://d.text"); bw.WriteLine("2"); bw.Close(); } else if (timer1.Enabled == true) { btnStart.Enabled = true; timer1.Enabled = false; textBox2.Text = "INC :Stop Increasing "; StreamWriter bw = new StreamWriter("d://d.text"); bw.WriteLine("3"); bw.Close(); } else if (timer1.Enabled == false && counter == 10) { textBox2.Text = "INC & DEC: Stop decreasing "; StreamWriter bw = new StreamWriter("d://d.text"); bw.WriteLine("5"); bw.Close(); } //Send stop information } private void btnReset_Click(object sender, EventArgs e) { textBox1.Text = "0"; textBox2.Text = "Initial state "; btnClose.Enabled = true; btnStart.Enabled = false; btnStop.Enabled = false; btnReset.Enabled = false; timer1.Enabled = false; counter = 0; num = 0; //reset StreamWriter bw = new StreamWriter("d://d.text"); bw.WriteLine("6"); bw.Close(); //Send the information reset } private void timer1_Tick(object sender, EventArgs e) { timer1.Interval = 3000; counter++; textBox1.Text = Convert.ToString(counter); if (counter > 9) { timer1.Enabled = false; textBox2.Text = "INC & DEC: Decreasing "; StreamWriter bw = new StreamWriter("d://d.text"); bw.WriteLine("1"); bw.Close(); } //In timer1 digital growth } private void timer2_Tick(object sender, EventArgs e) { if (File.Exists(@"d://c.text")) { //Whether have the information the file using (StreamReader br = new StreamReader("d://c.text")) { string line; if ((line = br.ReadLine()) != null) { num = Convert.ToInt32(line); br.Close(); } //Read in information } switch (num) //According to information change { case 0: btnClose_Click(sender,e); Close(); break; case 1: textBox2.Text = "INC & DEC :Increasing "; //Assigna start btnClose.Enabled = true; btnStop.Enabled = true; btnReset.Enabled = true; break; case 2: timer1.Enabled = true; // Assignb start textBox2.Text = "INC : Increasing "; break; case 3: btnStop.Enabled = false; textBox2.Text = "INC & DEC : Stop Increasing "; break; case 4: timer1.Enabled = false; btnStart.Enabled = true; btnStop.Enabled = false; textBox2.Text = "INC :Stop Increasing "; break; case 5: btnClose.Enabled = false; textBox2.Text = "INC & DEC :Stop Decreasing "; break; case 6: btnStop.Enabled = true; textBox2.Text = "INC & DEC :Decreasing "; break; case 7: textBox1.Text = "0"; textBox2.Text = "Initial State "; btnClose.Enabled = true; btnStart.Enabled = false; btnStop.Enabled = false; btnReset.Enabled = false; timer1.Enabled = false; counter = 0; num = 0; break; case 8: Close(); break; default: break; } File.Delete(@"d://c.text"); // Delete information file } } } }
其中有很多可以值得学习的,如果你愿意去重做一次。