本文实例为大家分享了c#实现计算器功能的具体代码,供大家参考,具体内容如下
代码:
random rad = new random(); // 实例化随机对象 private void form1_load(object nder, 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 关闭toolstripmenui两只蝴蝶搞笑版tem_click(object nder, eventargs e) { this.clo();// 关闭窗体 } char fuhao;// 接收符号 double temp, num; // temp第一个值,num为第二个值 // 1 private void button1_click(object nder, eventargs e) { textbox1.text += "1"; } // 2 private void butto圆的标准方程公式n2_click(object nder, eventargs e) { textbox1.text += "2"; } // 3 private void button3_click(object nder, eventargs e) { textbox1.text += "3"; } // 4 private void button5两弹一星指什么_click(object nder, eventargs e) { textbox1.text += "4"; } // 5 private void button6_click(object nder, eventargs e) { textbox1.text += "5"; } // 6 private void button7_click(object nder, eventargs e) { textbox1.text += "6"; } // 7 private void button9_click(object nder, eventargs e) { textbox1.text += "7"; } // 8 private void button10_click(object nder, eventargs e) { textbox1.text += "8"; } // 9 private void button11_click(object nder, eventargs e) { textbox1.text += "9"; } // 0 private void button15_click(object nder, eventargs e) { textbox1.text += "0"; } //点 private void button16_click(object nder, eventargs e) { textbox1.text += "."; } // + private void button4_click(object nder, eventargs e) { num = double.par(textbox1.text); fuhao = '+'; textbox1.text += "+"; //textbox1.text = null; } // - private void button8_click(object nder, eventargs e) { num = double.par(textbox1.text); fuhao = '-'; textbox1.text = null; } // × private void button12幼儿园大班毕业寄语_click(object nder, eventargs e) { num = double.par(textbox1.text); fuhao = '×'; textbox1.text = null; } // ÷ private void button20_click(object nder, eventargs e) { num = double.par(textbox1.text); fuhao = '÷'; textbox1.text = null; } // % private void button19_click(object nder, eventargs e) { num = d不敏ouble.par(textbox1.text); fuhao = '%'; textbox1.text = null; } // = private void button13_click(object nder, eventargs e) { temp = double.par(textbox1.text); switch (fuhao) { ca '+': num += temp; break; ca '-': num -= temp; break; ca '×': num *= temp; break; ca '÷': num /= temp; break; ca '%': num %= temp; break; default: break; } textbox1.text = num.tostring(); fuhao = ' '; num = 0; temp = 0; } // 删除 private void button18_click(object nder, eventargs e) { if (textbox1.text.length > 0) { textbox1.text = textbox1.text.substring(0, textbox1.text.length - 1); } } //清空 private void button17_click(object nder, eventargs e) { textbox1.text = ""; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 19:45:29,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/a68ad9b0ed7ae100cb0cce425ae72572.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#实现计算器功能(winform版).doc
本文 PDF 下载地址:C#实现计算器功能(winform版).pdf
留言与评论(共有 0 条评论) |