每日博客

 C#

编写一个计算器,练习在窗体上添加控件、调整控件的布局,设置或修改控件属性,

编写事件处理程序的方法。

(1)新建 windows 应用程序。在窗体 Form 上拖放一个 TextBox 控件、十六个 Button 控

件,整个窗体布局如下图所示。

 

 

(2)打开代码窗口,添加如下全局变量:

  double a = 0; 

  double b = 0;

  bool c = false;

  string d;

(3)双击”1”按钮,添加如下事件处理程序:

  private void button1_Click(object sender, EventArgs e)

  {

      if (c == true)

      {

           textBox1.Text = "";

           c = false;

      }

      textBox1.Text += "1";

  }

(4)双击”2”按钮,添加如下事件处理程序:

  private void button2_Click(object sender, EventArgs e)

  {

      if (c == true)

      {

           textBox2.Text = "";

           c = false;

      }

           textBox1.Text += "2";

  }

 (5)双击”3”按钮,添加如下事件处理程序:

  private void button3_Click(object sender, EventArgs e)

  {

      if (c == true)

      {

           textBox3.Text = "";

           c = false;

      }

      textBox1.Text += "3";

  }

 (6)双击”4”按钮,添加如下事件处理程序:

  private void button4_Click(object sender, EventArgs e)

  {

      if (c == true)

      {

           textBox1.Text = "";

           c = false;

      }

      textBox1.Text += "4";

  }

 (7)双击”5”按钮,添加如下事件处理程序:

  private void button5_Click(object sender, EventArgs e)

  {

      if (c == true)

      {

          textBox1.Text = "";

          c = false;

      }

      textBox1.Text += "5";

  }

  (8)双击”6”按钮,添加如下事件处理程序:

  private void button6_Click(object sender, EventArgs e)

  {

      if (c == true)

      {

          textBox1.Text = "";

          c = false;

      }

      textBox1.Text += "6";

  }

 (8)双击”7”按钮,添加如下事件处理程序:

  private void button7_Click(object sender, EventArgs e)

  {

      if (c == true)

      {

          textBox1.Text = "";

          c = false;

      }

      textBox1.Text += "7";

  }

  (10)双击”8”按钮,添加如下事件处理程序:

  private void button8_Click(object sender, EventArgs e)

  {

      if (c == true)

      {

          textBox1.Text = "";

          c = false;

      }

      textBox1.Text += "8";

  }

  (11)双击”9”按钮,添加如下事件处理程序:

  private void button9_Click(object sender, EventArgs e)

  {

      if (c == true)

      {

           textBox1.Text = "";

           c = false;

      }

      textBox1.Text += "9";

  }

  (12)双击”0”按钮,添加如下事件处理程序:

  private void button12_Click(object sender, EventArgs e)

  {

      if (c == true)

      {

          textBox1.Text = "";

          c = false;

      }

      textBox1.Text += "0";

      if (d == "/")

      {

          textBox1.Clear();

MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK,

MessageBoxIcon.Warning);

      }

  }

  (13)双击”+”按钮,添加如下事件处理程序:

  private void button13_Click(object sender, EventArgs e)

  {

      c = true;

      b = double.Parse(textBox1.Text);

      d = "+";

  }

  (14)双击”-”按钮,添加如下事件处理程序:

  private void button16_Click(object sender, EventArgs e)

  {

      c = true;

      b = double.Parse(textBox1.Text);

      d = "-";

  }

  (15)双击”*”按钮,添加如下事件处理程序:

  private void button15_Click(object sender, EventArgs e)

  {

      c = true;

      b = double.Parse(textBox1.Text);

      d = "*";

  }

  (16)双击”/”按钮,添加如下事件处理程序:

  private void button14_Click(object sender, EventArgs e)

  {

      c = true;

      b = double.Parse(textBox1.Text);

      d = "/";

  }

  (17)双击”=”按钮,添加如下事件处理程序:

  private void button17_Click(object sender, EventArgs e)

  {

      switch (d)

      {

          case "+": a = b + double.Parse(textBox1.Text); break;

          case "-": a = b - double.Parse(textBox1.Text); break;

          case "*": a = b * double.Parse(textBox1.Text); break;

          case "/": a = b / double.Parse(textBox1.Text); break;

      }

      textBox1.Text = a + "";

      c = true;

  }

(18)双击”c”按钮,添加如下事件处理程序:

  private void button18_Click(object sender, EventArgs e)

  {

      textBox1.Text = "";

  }

(19)单击启动调试工具,运行计算器。

(20)在计算器中,增加四个功能键:x2,sqrt,log, ln 四个键,分别计算求平方,开方,

log,ln 值。

namespace WindowsFormsApp1

{

    public partial class form1 : Form

    {

        double a = 0;

        double b = 0;

        bool c = false;

        string d;

        public form1(){InitializeComponent();}

        private void form1_Load(object sender, EventArgs e){}

        private void textBox1_TextChanged(object sender, EventArgs e){}

        private void button10_Click(object sender, EventArgs e)

        {

            if (c == true)    

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "2";

        }

        private void button1_Click(object sender, EventArgs e)

        {

            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "7";

        }

        private void button3_Click(object sender, EventArgs e)

        {

            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "8";

        }

        private void button4_Click(object sender, EventArgs e)

        {

            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "9";

        }

        private void button2_Click(object sender, EventArgs e)

        {

            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "4";

        }

        private void button7_Click(object sender, EventArgs e)

        {

            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "1";

        }

        private void button13_Click(object sender, EventArgs e)

        {

            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "3";

        }

        private void button9_Click(object sender, EventArgs e)

        {

            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "5";

        }

        private void button12_Click(object sender, EventArgs e)

        {

            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "6";

        }

        private void button8_Click(object sender, EventArgs e)

        {

            if (c == true)

            {

                textBox1.Text = "";

                c = false;

            }

            textBox1.Text += "0";

            if (d == "/")

            {

                textBox1.Clear();

                MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }

        }

        private void button14_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }

            else

            {

                c = true; b = double.Parse(textBox1.Text);

                d = "+";

            }

        }

        private void button19_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }

            else

            {

                c = true; b = double.Parse(textBox1.Text);

                d = "-";

            }

        }

        private void button18_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }

            else

            {

                c = true; b = double.Parse(textBox1.Text);

                d = "*";

            }

        }

        private void button6_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }

            else

            {

                c = true; b = double.Parse(textBox1.Text);

                d = "/";

            }

        }

        private void button11_Click(object sender, EventArgs e)

        {

            switch (d)

            {

                case "+": a = b + double.Parse(textBox1.Text); break;

                case "-": a = b - double.Parse(textBox1.Text); break;

                case "*": a = b * double.Parse(textBox1.Text); break;

                case "/": a = b / double.Parse(textBox1.Text); break;

                case "x^2": a = b * b; break;

                case "sqrt": a = Math.Sqrt(b); break;

                case "log": a = Math.Log10(b); break;

                case "ln": a = Math.Log(b); break;

            }

            textBox1.Text = a + "";

            c = true;

        }

        private void button17_Click(object sender, EventArgs e){textBox1.Text = "";}

        private void button5_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }

            else

            {

                c = true;

                b = double.Parse(textBox1.Text);

                d = "x^2";

            }

        }

        private void button15_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }

            else

            {

                c = true;

                b = double.Parse(textBox1.Text);

                d = "sqrt";

            }

        }

        private void button16_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }

            else

            {

                c = true;

                b = double.Parse(textBox1.Text);

                d = "log";

            }

        }

        private void button20_Click(object sender, EventArgs e)

        {

            if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }

            else

            {

                c = true;

                b = double.Parse(textBox1.Text);

                d = "ln";

            }

        }

    }

}

 

posted @ 2021-10-10 20:12  谦寻  阅读(49)  评论(0编辑  收藏  举报