返回顶部

一缕半夏微光

温柔半两,从容一生

导航

编写一个计算器,练习在窗体上添加控件、调整控件的布局,设置或修改控件属性, 编写事件处理程序的方法(C#)

(1)新建 windows 应用程序。在窗体 Form 上拖放一个 TextBox 控件、十六个 Button 控件,整个窗体布局如下图所示。 

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

1 double a = 0;  
2 double b = 0;
3 bool c = false;
4 string d;

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

1 private void button1_Click(object sender, EventArgs e) 
2   { 
3       if (c == true) 
4       { 
5            textBox1.Text = ""; 
6            c = false; 
7       } 
8       textBox1.Text += "1"; 
9   } 

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

1 private void button2_Click(object sender, EventArgs e) 
2   { 
3       if (c == true) 
4       { 
5            textBox2.Text = ""; 
6            c = false; 
7       } 
8            textBox1.Text += "2"; 
9   } 

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

1 private void button3_Click(object sender, EventArgs e) 
2   { 
3       if (c == true) 
4       { 
5            textBox3.Text = ""; 
6            c = false; 
7       } 
8       textBox1.Text += "3"; 
9   } 

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

1  private void button4_Click(object sender, EventArgs e) 
2   { 
3       if (c == true) 
4       { 
5            textBox1.Text = ""; 
6            c = false; 
7       } 
8       textBox1.Text += "4"; 
9   } 

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

1 private void button5_Click(object sender, EventArgs e) 
2   { 
3       if (c == true) 
4       { 
5           textBox1.Text = ""; 
6           c = false; 
7       } 
8       textBox1.Text += "5"; 
9   } 

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

1 private void button6_Click(object sender, EventArgs e) 
2   { 
3       if (c == true) 
4       { 
5           textBox1.Text = ""; 
6           c = false; 
7       } 
8       textBox1.Text += "6"; 
9   } 

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

1 private void button7_Click(object sender, EventArgs e) 
2   { 
3       if (c == true) 
4       { 
5           textBox1.Text = ""; 
6           c = false; 
7       } 
8       textBox1.Text += "7"; 
9   } 

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

1 private void button8_Click(object sender, EventArgs e) 
2   { 
3       if (c == true) 
4       { 
5           textBox1.Text = ""; 
6           c = false; 
7       } 
8       textBox1.Text += "8"; 
9   } 

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

1 private void button9_Click(object sender, EventArgs e) 
2   { 
3       if (c == true) 
4       { 
5            textBox1.Text = ""; 
6            c = false; 
7       } 
8       textBox1.Text += "9"; 
9   } 

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

 1 private void button12_Click(object sender, EventArgs e) 
 2   { 
 3       if (c == true) 
 4       { 
 5           textBox1.Text = ""; 
 6           c = false; 
 7       } 
 8       textBox1.Text += "0"; 
 9       if (d == "/") 
10       { 
11           textBox1.Clear(); 
12      MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK, 
13 MessageBoxIcon.Warning); 
14       } 
15   } 

 

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

1 private void button13_Click(object sender, EventArgs e) 
2   { 
3       c = true; 
4       b = double.Parse(textBox1.Text); 
5       d = "+"; 
6   } 

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

1 private void button16_Click(object sender, EventArgs e) 
2   { 
3       c = true; 
4       b = double.Parse(textBox1.Text); 
5       d = "-"; 
6   } 

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

1 private void button15_Click(object sender, EventArgs e) 
2   { 
3       c = true; 
4       b = double.Parse(textBox1.Text); 
5       d = "*"; 
6   } 

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

1 private void button14_Click(object sender, EventArgs e) 
2   { 
3       c = true; 
4       b = double.Parse(textBox1.Text); 
5       d = "/"; 
6   } 

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

 1 private void button17_Click(object sender, EventArgs e) 
 2   { 
 3       switch (d) 
 4       { 
 5           case "+": a = b + double.Parse(textBox1.Text); break; 
 6           case "-": a = b - double.Parse(textBox1.Text); break; 
 7           case "*": a = b * double.Parse(textBox1.Text); break; 
 8           case "/": a = b / double.Parse(textBox1.Text); break; 
 9       } 
10       textBox1.Text = a + ""; 
11       c = true; 
12   }

 

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

1 private void button18_Click(object sender, EventArgs e) 
2   { 
3       textBox1.Text = ""; 
4   }

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

根据上面的信息,我写出的效果为:

代码为:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace Net_3_1
 12 {
 13     public partial class Form1 : Form
 14     {
 15         double a = 0;
 16         double b = 0;
 17         bool c = false;
 18         string d;
 19 
 20         public Form1()
 21         {
 22             InitializeComponent();
 23         }
 24 
 25         private void Form1_Load(object sender, EventArgs e)
 26         {
 27 
 28         }
 29 
 30         private void button16_Click(object sender, EventArgs e)
 31         {
 32             if (c == true)
 33             {
 34                 textBox1.Text = "";
 35                 c = false;
 36             }
 37             textBox1.Text += "7"; 
 38         }
 39 
 40         private void button12_Click(object sender, EventArgs e)
 41         {
 42             if (c == true)
 43             {
 44                 textBox1.Text = "";
 45                 c = false;
 46             }
 47             textBox1.Text += "1"; 
 48         }
 49 
 50         private void button9_Click(object sender, EventArgs e)
 51         {
 52             if (c == true)
 53             {
 54                 textBox1.Text = "";
 55                 c = false;
 56             }
 57             textBox1.Text += "4"; 
 58         }
 59 
 60         private void button7_Click(object sender, EventArgs e)
 61         {
 62             if (c == true)
 63             {
 64                 textBox1.Text = "";
 65                 c = false;
 66             }
 67             textBox1.Text += "6"; 
 68         }
 69 
 70         private void button3_Click(object sender, EventArgs e)
 71         {
 72             switch (d)
 73             {
 74                 case "+": a = b + double.Parse(textBox1.Text); break;
 75                 case "-": a = b - double.Parse(textBox1.Text); break;
 76                 case "*": a = b * double.Parse(textBox1.Text); break;
 77                 case "/": a = b / double.Parse(textBox1.Text); break;
 78             }
 79             textBox1.Text = a + "";
 80             c = true; 
 81         }
 82 
 83         private void button5_Click(object sender, EventArgs e)
 84         {
 85             c = true;
 86             b = double.Parse(textBox1.Text);
 87             d = "-"; 
 88         }
 89 
 90         private void button15_Click(object sender, EventArgs e)
 91         {
 92             if (c == true)
 93             {
 94                 textBox1.Text = "";
 95                 c = false;
 96             }
 97             textBox1.Text += "8";
 98         }
 99 
100         private void button11_Click(object sender, EventArgs e)
101         {
102             if (c == true)
103             {
104                 textBox1.Text = "";
105                 c = false;
106             }
107             textBox1.Text += "2"; 
108         }
109 
110         private void button10_Click(object sender, EventArgs e)
111         {
112             if (c == true)
113             {
114                 textBox1.Text = "";
115                 c = false;
116             }
117             textBox1.Text += "3"; 
118         }
119 
120         private void button8_Click(object sender, EventArgs e)
121         {
122             if (c == true)
123             {
124                 textBox1.Text = "";
125                 c = false;
126             }
127             textBox1.Text += "5"; 
128         }
129 
130         private void button14_Click(object sender, EventArgs e)
131         {
132             if (c == true)
133             {
134                 textBox1.Text = "";
135                 c = false;
136             }
137             textBox1.Text += "9"; 
138         }
139 
140         private void button2_Click(object sender, EventArgs e)
141         {
142             if (c == true)
143             {
144                 textBox1.Text = "";
145                 c = false;
146             }
147             textBox1.Text += "0";
148             if (d == "/")
149             {
150                 textBox1.Clear();
151                 MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK,
152            MessageBoxIcon.Warning);
153             } 
154         }
155 
156         private void button6_Click(object sender, EventArgs e)
157         {
158             c = true;
159             b = double.Parse(textBox1.Text);
160             d = "+"; 
161         }
162 
163         private void button4_Click(object sender, EventArgs e)
164         {
165             c = true;
166             b = double.Parse(textBox1.Text);
167             d = "*"; 
168         }
169 
170         private void button13_Click(object sender, EventArgs e)
171         {
172             c = true;
173             b = double.Parse(textBox1.Text);
174             d = "/";
175         }
176 
177         private void button1_Click(object sender, EventArgs e)
178         {
179             textBox1.Text = ""; 
180         }
181     }
182 }

  (20)在计算器中,增加四个功能键:x2,sqrt,log, ln 四个键,分别计算求平方,开方,log,ln 值,将增加的代码写入实验报告。

①效果如下:

②代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace Net_3_1
 12 {
 13     public partial class Form1 : Form
 14     {
 15         double a = 0;
 16         double b = 0;
 17         bool c = false;
 18         string d;
 19 
 20         public Form1()
 21         {
 22             InitializeComponent();
 23         }
 24 
 25         private void Form1_Load(object sender, EventArgs e)
 26         {
 27 
 28         }
 29 
 30         private void button16_Click(object sender, EventArgs e)
 31         {
 32             if (c == true)
 33             {
 34                 textBox1.Text = "";
 35                 c = false;
 36             }
 37             textBox1.Text += "7";
 38         }
 39 
 40         private void button12_Click(object sender, EventArgs e)
 41         {
 42             if (c == true)
 43             {
 44                 textBox1.Text = "";
 45                 c = false;
 46             }
 47             textBox1.Text += "1";
 48         }
 49 
 50         private void button9_Click(object sender, EventArgs e)
 51         {
 52             if (c == true)
 53             {
 54                 textBox1.Text = "";
 55                 c = false;
 56             }
 57             textBox1.Text += "4";
 58         }
 59 
 60         private void button7_Click(object sender, EventArgs e)
 61         {
 62             if (c == true)
 63             {
 64                 textBox1.Text = "";
 65                 c = false;
 66             }
 67             textBox1.Text += "6";
 68         }
 69 
 70         private void button3_Click(object sender, EventArgs e)
 71         {
 72             switch (d)
 73             {
 74                 case "+": a = b + double.Parse(textBox1.Text); break;
 75                 case "-": a = b - double.Parse(textBox1.Text); break;
 76                 case "*": a = b * double.Parse(textBox1.Text); break;
 77                 case "/": a = b / double.Parse(textBox1.Text); break;
 78                 case "x^2": a = b * b; break;
 79                 case "sqrt": a = Math.Sqrt(b); break;
 80                 case "log": a = Math.Log10(b); break;
 81                 case "ln": a = Math.Log(b); break;
 82             }
 83             textBox1.Text = a + "";
 84             c = true;
 85         }
 86 
 87         private void button5_Click(object sender, EventArgs e)
 88         {
 89             c = true;
 90             b = double.Parse(textBox1.Text);
 91             d = "-";
 92         }
 93 
 94         private void button15_Click(object sender, EventArgs e)
 95         {
 96             if (c == true)
 97             {
 98                 textBox1.Text = "";
 99                 c = false;
100             }
101             textBox1.Text += "8";
102         }
103 
104         private void button11_Click(object sender, EventArgs e)
105         {
106             if (c == true)
107             {
108                 textBox1.Text = "";
109                 c = false;
110             }
111             textBox1.Text += "2";
112         }
113 
114         private void button10_Click(object sender, EventArgs e)
115         {
116             if (c == true)
117             {
118                 textBox1.Text = "";
119                 c = false;
120             }
121             textBox1.Text += "3";
122         }
123 
124         private void button8_Click(object sender, EventArgs e)
125         {
126             if (c == true)
127             {
128                 textBox1.Text = "";
129                 c = false;
130             }
131             textBox1.Text += "5";
132         }
133 
134         private void button14_Click(object sender, EventArgs e)
135         {
136             if (c == true)
137             {
138                 textBox1.Text = "";
139                 c = false;
140             }
141             textBox1.Text += "9";
142         }
143 
144         private void button2_Click(object sender, EventArgs e)
145         {
146             if (c == true)
147             {
148                 textBox1.Text = "";
149                 c = false;
150             }
151             textBox1.Text += "0";
152             if (d == "/")
153             {
154                 textBox1.Clear();
155                 MessageBox.Show("除数不能为零", "错误提示", MessageBoxButtons.OK,
156            MessageBoxIcon.Warning);
157             }
158         }
159 
160         private void button6_Click(object sender, EventArgs e)
161         {
162             c = true;
163             b = double.Parse(textBox1.Text);
164             d = "+";
165         }
166 
167         private void button4_Click(object sender, EventArgs e)
168         {
169             c = true;
170             b = double.Parse(textBox1.Text);
171             d = "*";
172         }
173 
174         private void button13_Click(object sender, EventArgs e)
175         {
176             c = true;
177             b = double.Parse(textBox1.Text);
178             d = "/";
179         }
180 
181         private void button1_Click(object sender, EventArgs e)
182         {
183             textBox1.Text = "";
184         }
185 
186         private void button20_Click(object sender, EventArgs e)
187         {
188 
189             if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
190             else
191             {
192                 c = true;
193                 b = double.Parse(textBox1.Text);
194                 d = "x^2";
195             }
196         }
197 
198         private void button18_Click(object sender, EventArgs e)
199         {
200             if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
201             else
202             {
203                 c = true;
204                 b = double.Parse(textBox1.Text);
205                 d = "sqrt";
206             }
207         }
208 
209         private void button17_Click(object sender, EventArgs e)
210         {
211             if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
212             else
213             {
214                 c = true;
215                 b = double.Parse(textBox1.Text);
216                 d = "log";
217             }
218         }
219 
220         private void button19_Click(object sender, EventArgs e)
221         {
222             if (textBox1.Text == "") { MessageBox.Show("请先输入值再计算!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
223             else
224             {
225                 c = true;
226                 b = double.Parse(textBox1.Text);
227                 d = "ln";
228             }
229         }
230     }
231 }

参考链接:https://blog.csdn.net/lady_killer9/article/details/78345167

posted on 2021-11-17 21:26  一缕半夏微光  阅读(899)  评论(0编辑  收藏  举报