C#实现计算器功能(winform版)
程序员文章站
2022-06-10 09:16:04
本文实例为大家分享了c#实现计算器功能的具体代码,供大家参考,具体内容如下代码:random rad = new random(); // 实例化随机对象 private void fo...
本文实例为大家分享了c#实现计算器功能的具体代码,供大家参考,具体内容如下
代码:
random rad = new random(); // 实例化随机对象 private void form1_load(object sender, eventargs e) { this.left = screen.primaryscreen.workingarea.width / 2 - this.width / 2; this.top = screen.primaryscreen.workingarea.height / 2 - this.height / 2; this.text = "计算器"; textbox1.readonly =true;// 文本框无法输入字符 foreach (control ctl in this.controls) { // 获取所有按钮 改变背景颜色和数字和符号颜色 if (ctl is button) { button btn = ctl as button; btn.backcolor = color.fromargb(rad.next(256),rad.next(256),rad.next(256),rad.next(256)); btn.forecolor = color.fromargb(rad.next(256), rad.next(256), rad.next(256)); } } } private void 关闭toolstripmenuitem_click(object sender, eventargs e) { this.close();// 关闭窗体 } char fuhao;// 接收符号 double temp, num; // temp第一个值,num为第二个值 // 1 private void button1_click(object sender, eventargs e) { textbox1.text += "1"; } // 2 private void button2_click(object sender, eventargs e) { textbox1.text += "2"; } // 3 private void button3_click(object sender, eventargs e) { textbox1.text += "3"; } // 4 private void button5_click(object sender, eventargs e) { textbox1.text += "4"; } // 5 private void button6_click(object sender, eventargs e) { textbox1.text += "5"; } // 6 private void button7_click(object sender, eventargs e) { textbox1.text += "6"; } // 7 private void button9_click(object sender, eventargs e) { textbox1.text += "7"; } // 8 private void button10_click(object sender, eventargs e) { textbox1.text += "8"; } // 9 private void button11_click(object sender, eventargs e) { textbox1.text += "9"; } // 0 private void button15_click(object sender, eventargs e) { textbox1.text += "0"; } //点 private void button16_click(object sender, eventargs e) { textbox1.text += "."; } // + private void button4_click(object sender, eventargs e) { num = double.parse(textbox1.text); fuhao = '+'; textbox1.text += "+"; //textbox1.text = null; } // - private void button8_click(object sender, eventargs e) { num = double.parse(textbox1.text); fuhao = '-'; textbox1.text = null; } // × private void button12_click(object sender, eventargs e) { num = double.parse(textbox1.text); fuhao = '×'; textbox1.text = null; } // ÷ private void button20_click(object sender, eventargs e) { num = double.parse(textbox1.text); fuhao = '÷'; textbox1.text = null; } // % private void button19_click(object sender, eventargs e) { num = double.parse(textbox1.text); fuhao = '%'; textbox1.text = null; } // = private void button13_click(object sender, eventargs e) { temp = double.parse(textbox1.text); switch (fuhao) { case '+': num += temp; break; case '-': num -= temp; break; case '×': num *= temp; break; case '÷': num /= temp; break; case '%': num %= temp; break; default: break; } textbox1.text = num.tostring(); fuhao = ' '; num = 0; temp = 0; } // 删除 private void button18_click(object sender, eventargs e) { if (textbox1.text.length > 0) { textbox1.text = textbox1.text.substring(0, textbox1.text.length - 1); } } //清空 private void button17_click(object sender, eventargs e) { textbox1.text = ""; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 详解C# winform ListView的基本操作
下一篇: C#算法之各位相加