C#窗體實現計算機案例

一.編程思路

1.計算器頁面的簡單佈局;
2.按下對應的數字可以進行顯示;
3.第二次輸入數時,將第一次輸入的數保存並清空文本框;
4.對數值可以進行運算並進行顯示;

二.編程核心

1.按下對應的數字進行顯示;
2.輸入數字進行存儲;
3.取出存儲數字並進行相應的計算; 4.運算結果的顯示;

三.代碼實現

1.界面佈局:(控件:button,textbox)

在這裏插入圖片描述

2.窗體加載時代碼:
 private void Form1_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();//設置窗體居中
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;//設置窗體邊框樣式
            textBox1.ReadOnly = true;//設置爲僅讀
            textBox2.Focus();//獲取焦點
        }
3.對應的數字進行顯示:(每個數字都要進行設置,在此只舉例一個,下面的依照此格式)
  //7
        private void button5_Click(object sender, EventArgs e)
        {
         //設置按鍵在textbox顯示相應的數字
            textBox1.Text += 7;
            textBox2.Text += 7;
        }
4.按下運算符時,清空文本框:(每個數字都要進行設置,在此只舉例一個,下面的依照此格式)
 bool bt = false;//爲判斷是否按下運算按鈕,然後清空textbox1的內容
  //7
        private void button5_Click(object sender, EventArgs e)
        {
            if (bt==true)
            {
                bt = false;
                textBox1.Text = "";
            }
            //設置按鍵在textbox顯示相應的數字
            textBox1.Text += 7;
            textBox2.Text += 7;
        }
5.按下運算符對第一次輸入的數字進行保存,並在textbox2顯示對應的運算符:
double num1,num2;//存儲第一次和第二次textbox1的內容
string type;//記錄輸入符號
 //+
        private void button8_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            bt = true;
            type = "+";
            textBox2.Text +=type;
        }
         //-
        private void button4_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            bt = true;
            type = "-";
            textBox2.Text += type;
        }
         //×
        private void button3_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            bt = true;
            type = "×";
            textBox2.Text += type ;
        }
         //÷
        private void button2_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            bt = true;
            type = "÷";
            textBox2.Text += type ;
        }
          //%
        private void button18_Click(object sender, EventArgs e)
        {
            num1 = double.Parse(textBox1.Text);
            bt = true;
            type = "%";
            textBox2.Text += type;
        }
6.按下等號時,進行相應的運算並顯示結果:
 //=
        private void button15_Click(object sender, EventArgs e)
        {
           
            num2=double.Parse(textBox1.Text);//存儲第二次的輸入數
            switch (type)
            {
                case "+":
                    textBox1.Text = (num1 + num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;
                case "-":
                    textBox1.Text = (num1 - num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;
                case "×":
                    textBox1.Text = (num1 * num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;
                case "÷":
                    if (num2 != 0)//被除數不能爲零,進行判斷
                    {
                        textBox1.Text = (num1 / num2).ToString();
                        textBox2.Text += "=" + textBox1.Text;
                    }
                    else//如果爲零進行報錯,並清空輸入的數值
                        MessageBox.Show("輸入錯誤!");
                        textBox1.Text="";
                        textBox2.Text="";
                    break;
                case "%":
                    textBox1.Text = (num1 % num2).ToString();
                    textBox2.Text += "=" + textBox1.Text;
                    break;
                default:
                    break;
            }
        }
7.點擊ESC時清空輸入的內容:
 //ESC
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }

四.運行結果:

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章