form1
namespace 数学测试{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } Class1 fengzhuang = new Class1();//调用封装的内容 string lujing = "D:\txt"; public static int Count = 0; // 题目出的数量 public static int cuo = 0; public static int dui = 0; public static int jishi; public static int time; public static int sum; int i = 0; private void textBox9_TextChanged(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e)//打开文件 { OpenFileDialog baocun = new OpenFileDialog(); baocun.Filter = "TXT文件(*.txt)|*.txt"; if (baocun.ShowDialog() == DialogResult.OK) { lujing = baocun.FileName; this.richTextBox1.LoadFile(baocun.FileName, RichTextBoxStreamType.RichText); daikai.Enabled = false; save.Enabled = false; MessageBox.Show("读取成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } private void button1_Click(object sender, EventArgs e) { Count++; StreamWriter xie = File.AppendText("xie.txt"); xie.WriteLine(t1.Text); xie.Close(); StreamWriter xie1 = File.AppendText("xie1.txt"); xie1.WriteLine(t2.Text); xie1.Close(); StreamWriter xie2 = File.AppendText("xie2.txt"); xie2.WriteLine(t3.Text); xie2.Close(); richTextBox1.Text += t1.Text + t2.Text + t3.Text + label1.Text + "\n"; t1.Clear(); t2.Clear(); t3.Clear(); } private void richTextBox1_TextChanged(object sender, EventArgs e) { save.Enabled = true; if (this.richTextBox1.Text == "" || this.richTextBox1.Text == null) { daikai.Enabled = true; } } private void save_Click(object sender, EventArgs e)//保存文件 { SaveFileDialog baocun = new SaveFileDialog(); baocun.Filter = "TXT文件(*.txt)|*.txt"; if (File.Exists(lujing)) { this.richTextBox1.LoadFile(lujing, RichTextBoxStreamType.RichText); MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); this.richTextBox1.Clear(); save.Enabled = false; } else { if (baocun.ShowDialog() == DialogResult.OK) { this.richTextBox1.SaveFile(baocun.FileName, RichTextBoxStreamType.RichText); MessageBox.Show("保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); this.richTextBox1.Clear(); save.Enabled = false; } } } private void kaishi_Click(object sender, EventArgs e) { string[] T1 = new string[100]; T1 = File.ReadAllLines("xie.txt"); tB1.Text = T1[0]; string[] T2 = new string[100]; T2 = File.ReadAllLines("xie1.txt"); tB2.Text = T2[0]; string[] T3 = new string[100]; T3 = File.ReadAllLines("xie2.txt"); tB3.Text = T3[0]; int daojishi; try { daojishi = int.Parse(this.shijian.Text); } catch (Exception ex) { MessageBox.Show("输入错误"); return; } jishi = daojishi; this.timer1.Interval = 1000; this.timer1.Enabled = true; this.timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { time = Convert.ToInt32(shijian.Text); if (jishi <= 0) { timer1.Enabled = false; MessageBox.Show("答题时间到!"); daan.Enabled = false; Form2 frm2 = new Form2(); frm2.ShowDialog(); } this.label3.Text = "剩余时间" + jishi.ToString() + "秒"; jishi--; } private void daan_KeyDown(object sender, KeyEventArgs e) { fengzhuang.a = (int.Parse(tB1.Text)); fengzhuang.b = (int.Parse(tB3.Text)); Class1.fh = tB2.Text; fengzhuang.jisuan(); int result; if (e.KeyCode == Keys.Enter) { if (int.TryParse(daan.Text, out result) == false) { MessageBox.Show("请输入数字"); } daan.Text += sum ; if (daan.Text == fengzhuang.sum.ToString()) { MessageBox.Show("回答正确"); Count++; dui++; } else { cuo++; } i++; daan.Clear(); string[] T1 = new string[100]; T1 = File.ReadAllLines("xie.txt"); tB1.Text = T1[i]; string[] T2 = new string[100]; T2 = File.ReadAllLines("xie1.txt"); tB2.Text = T2[i]; string[] T3 = new string[100]; T3 = File.ReadAllLines("xie2.txt"); tB3.Text = T3[i]; } } private void dakaishiti_Click(object sender, EventArgs e) { OpenFileDialog dakai = new OpenFileDialog(); dakai.Filter = "TXT文件(*.txt)|*.txt"; if (dakai.ShowDialog() == DialogResult.OK) { lujing = dakai.FileName; this.richTextBox2.LoadFile(dakai.FileName, RichTextBoxStreamType.RichText); save.Enabled = false; kaishi.Enabled = false; MessageBox.Show("打开成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } private void daorushiti_Click(object sender, EventArgs e) { richTextBox2.Text = richTextBox1.Text; } private void jieshu_Click(object sender, EventArgs e) { daan.Enabled = false; Form2 frm2 = new Form2(); frm2.ShowDialog(); } }}
form2
namespace 数学测试{ public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { textBox2.Text = Form1.dui.ToString(); textBox1.Text = Form1.Count.ToString(); textBox3.Text = ((Form1.Count - (double)(Form1.dui))).ToString(); textBox4.Text = ((Form1.dui / (double)(Form1.cuo)) * 100).ToString("f2") + "%"; } private void button1_Click(object sender, EventArgs e) { Class1 fengzhuang = new Class1(); Application.Exit(); fengzhuang.chongzhi(); } }}
类的封装
namespace 数学测试{ class Class1 { public int a, b; public int sum; public static string fh; public void chongzhi() //重新输入新的内容 { File.WriteAllText("writer.txt", string.Empty); File.WriteAllText("writer1.txt", string.Empty); File.WriteAllText("xie.txt", string.Empty); File.WriteAllText("xie1.txt", string.Empty); File.WriteAllText("xie2.txt", string.Empty); } public void jisuan() //计算方法 { switch (fh) { case "+": sum = a + b ; break; case "-": sum = a - b ; break; case "*": sum = a*b; break; case"/": sum = a / b; break; default: break; } } }}
程序调试