博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用类做封装
阅读量:4967 次
发布时间:2019-06-12

本文共 8079 字,大约阅读时间需要 26 分钟。

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;            }        }    }}

程序调试

 

转载于:https://www.cnblogs.com/Meng-7/p/5033806.html

你可能感兴趣的文章
关于分工的思考 (Thoughts on Division of Labor)
查看>>
Python实现快速排序
查看>>
RabbitMQ 原文译05--Topics
查看>>
hive mapreduce reducer 调优
查看>>
configure: error: jpeglib.h not found.
查看>>
字节流与消息
查看>>
【Python基础】13_Python中的PASS
查看>>
【Python基础】15_Python中的变量-进阶
查看>>
socket编程---UDP
查看>>
之江学院第0届校赛 qwb去面试 (找规律)
查看>>
数据库知识点总结 - 1
查看>>
HTML5中的绘图SVG VS Canvas
查看>>
memcached集群和一致性哈希算法
查看>>
在窗口中绘画
查看>>
信息安全系统设计基础第十一周学习总结
查看>>
linux运行级别
查看>>
数据库命名及设计规范
查看>>
解决Mac下MX4手机无法连接adb问题之解决方案
查看>>
Web —— java web 项目开发 笔记
查看>>
Python搜索目录下指定的文件,并返回绝对路径(包括子目录)
查看>>