WinForm 小练习 计算器
Posted 马MZJ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WinForm 小练习 计算器相关的知识,希望对你有一定的参考价值。
1 string a = "0"; int b; int c; bool d = true; string f = ""; 2 /// <summary> 3 /// 按键8 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 private void button2_Click(object sender, EventArgs e) 8 { 9 if (a != "") 10 { 11 a = textBox2.Text; 12 } 13 if (a == "0" || a == "") 14 { 15 textBox2.Text = "8"; 16 a = textBox2.Text; 17 } 18 else 19 { 20 textBox2.Text += "8"; 21 } 22 if (textBox1.Text == "") 23 { 24 c = Convert.ToInt32(textBox2.Text); 25 d = true; 26 } 27 else 28 { 29 d = false; 30 } 31 b = Convert.ToInt32(textBox2.Text); 32 } 33 /// <summary> 34 /// 按键0 35 /// </summary> 36 /// <param name="sender"></param> 37 /// <param name="e"></param> 38 private void button13_Click(object sender, EventArgs e) 39 { 40 if (a != "") 41 { 42 a = textBox2.Text; 43 } 44 if (a == "0" || a == "") 45 { 46 textBox2.Text = "0"; 47 a = textBox2.Text; 48 } 49 else 50 { 51 textBox2.Text += "0"; 52 } 53 if (textBox1.Text == "") 54 { 55 c = Convert.ToInt32(textBox2.Text); 56 d = true; 57 } 58 else 59 { 60 d = false; 61 } 62 b = Convert.ToInt32(textBox2.Text); 63 } 64 /// <summary> 65 /// 按键+ 66 /// </summary> 67 /// <param name="sender"></param> 68 /// <param name="e"></param> 69 private void button8_Click(object sender, EventArgs e) 70 { 71 textBox1.Text += textBox2.Text + "-"; 72 if (d == false) 73 { 74 if (f == "1") 75 { 76 c = c + b; 77 } 78 else if (f == "3") 79 { 80 c = c * b; 81 } 82 else if (f == "4") 83 { 84 c = c / b; 85 } 86 else if (f == "2") 87 { 88 c = c - b; 89 } 90 } 91 textBox2.Text = c.ToString(); 92 a = ""; 93 f = "2"; 94 } 95 /// <summary> 96 /// 按键/ 97 /// </summary> 98 /// <param name="sender"></param> 99 /// <param name="e"></param> 100 private void button16_Click(object sender, EventArgs e) 101 { 102 textBox1.Text += textBox2.Text + "/"; 103 if (d == false) 104 { 105 if (f == "1") 106 { 107 c = c + b; 108 } 109 else if (f == "2") 110 { 111 c = c - b; 112 } 113 else if (f == "3") 114 { 115 c = c * b; 116 } 117 else if (f == "4") 118 { 119 c = c / b; 120 } 121 } 122 textBox2.Text = c.ToString(); 123 a = ""; 124 f = "4"; 125 } 126 /// <summary> 127 /// 按键1 128 /// </summary> 129 /// <param name="sender"></param> 130 /// <param name="e"></param> 131 private void textBox1_TextChanged(object sender, EventArgs e) 132 { 133 134 } 135 136 private void button9_Click(object sender, EventArgs e) 137 { 138 if (a != "") 139 { 140 a = textBox2.Text; 141 } 142 if (a == "0" || a == "") 143 { 144 textBox2.Text = "1"; 145 a = textBox2.Text; 146 } 147 else 148 { 149 textBox2.Text += "1"; 150 } 151 if (textBox1.Text == "") 152 { 153 c = Convert.ToInt32(textBox2.Text); 154 d = true; 155 } 156 else 157 { 158 d = false; 159 } 160 b = Convert.ToInt32(textBox2.Text); 161 } 162 /// <summary> 163 /// 按键2 164 /// </summary> 165 /// <param name="sender"></param> 166 /// <param name="e"></param> 167 private void button10_Click(object sender, EventArgs e) 168 { 169 if (a != "") 170 { 171 a = textBox2.Text; 172 } 173 if (a == "0" || a == "") 174 { 175 textBox2.Text = "2"; 176 a = textBox2.Text; 177 } 178 else 179 { 180 textBox2.Text += "2"; 181 } 182 if (textBox1.Text == "") 183 { 184 c = Convert.ToInt32(textBox2.Text); 185 d = true; 186 } 187 else 188 { 189 d = false; 190 } 191 b = Convert.ToInt32(textBox2.Text); 192 } 193 /// <summary> 194 /// 按键3 195 /// </summary> 196 /// <param name="sender"></param> 197 /// <param name="e"></param> 198 private void button11_Click(object sender, EventArgs e) 199 { 200 if (a != "") 201 { 202 a = textBox2.Text; 203 } 204 if (a == "0" || a == "") 205 { 206 textBox2.Text = "3"; 207 a = textBox2.Text; 208 } 209 else 210 { 211 textBox2.Text += "3"; 212 } 213 if (textBox1.Text == "") 214 { 215 c = Convert.ToInt32(textBox2.Text); 216 d = true; 217 } 218 else 219 { 220 d = false; 221 } 222 b = Convert.ToInt32(textBox2.Text); 223 } 224 /// <summary> 225 /// 按键4 226 /// </summary> 227 /// <param name="sender"></param> 228 /// <param name="e"></param> 229 private void button5_Click(object sender, EventArgs e) 230 { 231 if (a != "") 232 { 233 a = textBox2.Text; 234 } 235 if (a == "0" || a == "") 236 { 237 textBox2.Text = "4"; 238 a = textBox2.Text; 239 } 240 else 241 { 242 textBox2.Text += "4"; 243 } 244 if (textBox1.Text == "") 245 { 246 c = Convert.ToInt32(textBox2.Text); 247 d = true; 248 } 249 else 250 { 251 d = false; 252 } 253 254 b = Convert.ToInt32(textBox2.Text); 255 } 256 /// <summary> 257 /// 按键5 258 /// </summary> 259 /// <param name="sender"></param> 260 /// <param name="e"></param> 261 private void button6_Click(object sender, EventArgs e) 262 { 263 if (a != "") 264 { 265 a = textBox2.Text; 266 } 267 if (a == "0" || a == "") 268 { 269 textBox2.Text = "5"; 270 a = textBox2.Text; 271 } 272 else 273 { 274 textBox2.Text += "5"; 275 } 276 if (textBox1.Text == "") 277 { 278 c = Convert.ToInt32(textBox2.Text); 279 d = true; 280 } 281 else 282 { 283 d = false; 284 } 285 b = Convert.ToInt32(textBox2.Text); 286 } 287 /// <summary> 288 /// 按键6 289 /// </summary> 290 /// <param name="sender"></param> 291 /// <param name="e"></param> 292 private void button7_Click(object sender, EventArgs e) 293 { 294 if (a != "") 295 { 296 a = textBox2.Text; 297 } 298 if (a == "0" || a == "") 299 { 300 textBox2.Text = "6"; 301 a = textBox2.Text; 302 } 303 else 304 { 305 textBox2.Text += "6"; 306 } 307 if (textBox1.Text == "") 308 { 309 c = Convert.ToInt32(textBox2.Text); 310 d = true; 311 } 312 else 313 { 314 d = false; 315 } 316 b = Convert.ToInt32(textBox2.Text); 317 } 318 /// <summary> 319 /// 按键7 320 /// </summary> 321 /// <param name="sender"></param> 322 /// <param name="e"></param> 323 private void button1_Click(object sender, EventArgs e) 324 { 325 if (a != "") 326 { 327 a = textBox2.Text; 328 } 329 if (a == "0" || a == "") 330 { 331 textBox2.Text = "7"; 332 a = textBox2.Text; 333 } 334 else 335 { 336 textBox2.Text += "7"; 337 } 338 if (textBox1.Text == "") 339 { 340 c = Convert.ToInt32(textBox2.Text); 341 d = true; 342 } 343 else 344 { 345 d = false; 346 } 347 b = Convert.ToInt32(textBox2.Text); 348 } 349 /// <summary> 350 /// 按键9 351 /// </summary> 352 /// <param name="sender"></param> 353 /// <param name="e"></param> 354 private void button3_Click(object sender, EventArgs e) 355 { 356 if (a != "") 357 { 358 a = textBox2.Text; 359 } 360 if (a == "0" || a == "") 361 { 362 textBox2.Text = "9"; 363 a = textBox2.Text; 364 } 365 else 366 { 367 textBox2.Text += "9"; 368 } 369 if (textBox1.Text == "") 370 { 371 c = Convert.ToInt32(textBox2.Text); 372 } 373 else 374 { 375 d = false; 376 } 377 b = Convert.ToInt32(textBox2.Text); 378 } 379 /// <summary> 380 /// 按键+ 381 /// </summary> 382 /// <param name="sender"></param> 383 /// <param name="e"></param> 384 private void button4_Click(object sender, EventArgs e) 385 { 386 textBox1.Text += textBox2.Text + "+"; 387 if (d == false) 388 { 389 if (f == "2") 390 { 391 c = c - b; 392 } 393 else if (f == "3") 394 { 395 c = c * b; 396 } 397 else if (f == "4") 398 { 399 c = c / b; 400 } 401 else if (f == "1") 402 { 403 c = c + b; 404 } 405 } 406 textBox2.Text = c.ToString(); 407 a = ""; 408 f = "1"; 409 } 410 /// <summary> 411 /// 按键* 412 /// </summary> 413 /// <param name="sender"></param> 414 /// <param name="e"></param> 415 private void button12_Click(object sender, EventArgs e) 416 { 417 textBox1.Text += textBox2.Text + "*"; 418 if (d == false) 419 { 420 if (f == "1") 421 { 422 c = c + b; 423 } 424 else if (f == "2") 425 { 426 c = c - b; 427 } 428 else if (f == "4") 429 { 430 c = c / b; 431 } 432 else if (f == "3") 433 { 434 c = c * b; 435 } 436 } 437 textBox2.Text = c.ToString(); 438 a = ""; 439 f = "3"; 440 } 441 /// <summary> 442 /// 按键清空 443 /// </summary> 444 /// <param name="sender"></param> 445 /// <param name="e"></param> 446 private void button15_Click(object sender, EventArgs e) 447 { 448 textBox1.Text = ""; 449 textBox2.Text = "0"; 450 f = ""; 451 c = 0; 452 b = 0; 453 } 454 /// <summary> 455 /// 按键= 456 /// </summary> 457 /// <param name="sender"></param> 458 /// <param name="e"></param> 459 private void button14_Click(object sender, EventArgs e) 460 { 461 if (f == "1") 462 { 463 textBox1.Text = ""; 464 textBox2.Text = (c + b).ToString(); 465 } 466 else if (f == "2") 467 { 468 textBox1.Text = ""; 469 textBox2.Tex以上是关于WinForm 小练习 计算器的主要内容,如果未能解决你的问题,请参考以下文章
Spring MVC 框架学习---- 后端接口小练习(计算器与登陆拦截)