首页 > 代码库 > winform制作简单计算器
winform制作简单计算器
1 public Form1() 2 { 3 InitializeComponent(); 4 textBox2.Text = "0";//主显示屏 5 textBox1.Text = "";//顶端副显示屏 6 textBox3.Text = "";//中间变量,隐藏 7 8 } 9 10 private void Form1_Load(object sender, EventArgs e) 11 { 12 13 } 14 //数字按键 15 private void button21_Click(object sender, EventArgs e) 16 { 17 if (sender is Button) 18 { 19 Button btn = sender as Button; 20 if (textBox2.Text == "0")//如果主屏幕为0 21 { 22 textBox2.Text = btn.Text;//输入几就显示几 23 } 24 else 25 { 26 if (yunsuanfu)//上个按键是运算符 27 { 28 textBox2.Text = btn.Text;//主屏幕只显示本次输入的数字 29 yunsuanfu = false; 30 } 31 else 32 { 33 textBox2.Text += btn.Text;//上个按键不是运算符,主屏幕右侧累添上这个数字 34 } 35 } 36 } 37 } 38 39 //加减乘除 40 private bool yunsuanfu = false; 41 private double jieguo1 = 0;//点击加减乘除运算符显示的结果 42 private string text = ""; 43 private void button26_Click(object sender, EventArgs e) 44 { 45 if (sender is Button) 46 { 47 Button btn = sender as Button; 48 text = btn.Text; 49 if(textBox3.Text=="") 50 { 51 textBox3.Text = btn.Text; 52 } 53 //判断上一步点击的是否是运算符 54 if (yunsuanfu==false)//不是运算符 55 { 56 textBox1.Text += textBox2.Text + text; 57 yunsuanfu = true; 58 if(textBox3.Text=="+") 59 { 60 jieguo1 += Convert.ToDouble(textBox2.Text); 61 textBox2.Text = jieguo1.ToString(); 62 } 63 else if (textBox3.Text == "-") 64 { 65 jieguo1 -= Convert.ToDouble(textBox2.Text); 66 textBox2.Text = jieguo1.ToString(); 67 } 68 else if (textBox3.Text == "*") 69 { 70 if (jieguo1 != 0) 71 { 72 jieguo1 = jieguo1 * Convert.ToDouble(textBox2.Text); 73 } 74 else 75 { 76 jieguo1 = Convert.ToDouble(textBox2.Text); 77 } 78 textBox2.Text = jieguo1.ToString(); 79 } 80 else if (textBox3.Text == "/") 81 { 82 if (Convert.ToDouble(textBox2.Text) != 0) 83 { 84 if (jieguo1 != 0) 85 { 86 jieguo1 = jieguo1 / Convert.ToDouble(textBox2.Text); 87 } 88 else 89 { 90 jieguo1 = Convert.ToDouble(textBox2.Text); ; 91 } 92 textBox2.Text = jieguo1.ToString(); 93 } 94 else 95 { 96 textBox2.Text = "除数不能为0"; 97 } 98 } 99 100 textBox3.Text = text; 101 } 102 else 103 { 104 if (textBox1.Text.Length > 0) 105 { 106 textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1) + text; 107 textBox3.Text = text; 108 } 109 else 110 { 111 textBox3.Text = text; 112 } 113 yunsuanfu = false; 114 } 115 } 116 } 117 118 //小数点 119 private void button25_Click(object sender, EventArgs e) 120 { 121 //判断上一步点击的是否是运算符 122 if (yunsuanfu == false)//不是运算符 123 { 124 if (textBox2.Text.Contains(".") == false) 125 { 126 textBox2.Text = textBox2.Text + "."; 127 } 128 } 129 else 130 { 131 textBox2.Text = "0."; 132 yunsuanfu = false; 133 } 134 } 135 136 //等号 137 private double jieguo = 0; 138 private string text4 = ""; 139 private string text5 = ""; 140 private void button28_Click(object sender, EventArgs e) 141 { 142 jieguo = jieguo1; 143 jieguo1 = 0; 144 text4 = textBox3.Text; 145 textBox3.Text = ""; 146 text = ""; 147 if (text4 == "") 148 { 149 text4 = button28.Text; 150 } 151 152 textBox1.Clear(); 153 textBox1.Text = ""; 154 //判断上一步点击的是否是运算符 155 if (yunsuanfu == false)//不是运算符 156 { 157 yunsuanfu = true; 158 if (text4 == "+") 159 { 160 jieguo += Convert.ToDouble(textBox2.Text); 161 textBox2.Text = jieguo.ToString(); 162 } 163 else if (text4 == "-") 164 { 165 jieguo -= Convert.ToDouble(textBox2.Text); 166 textBox2.Text = jieguo.ToString(); 167 } 168 else if (text4 == "*") 169 { 170 if (jieguo != 0) 171 { 172 jieguo = jieguo * Convert.ToDouble(textBox2.Text); 173 } 174 else 175 { 176 jieguo = Convert.ToDouble(textBox2.Text); 177 } 178 textBox2.Text = jieguo.ToString(); 179 } 180 else if (text4 == "/") 181 { 182 if (textBox2.Text != "0") 183 { 184 if (jieguo != 0) 185 { 186 jieguo = jieguo / Convert.ToDouble(textBox2.Text); 187 } 188 else 189 { 190 jieguo = Convert.ToDouble(textBox2.Text); ; 191 } 192 textBox2.Text = jieguo.ToString(); 193 } 194 else 195 { 196 textBox2.Text = "除数不能为0"; 197 } 198 } 199 else if (text4 == "=") 200 { 201 jieguo = jieguo; 202 } 203 204 text5 = text4; 205 text4 = button28.Text; 206 } 207 else 208 { 209 yunsuanfu = false; 210 double jishu = 0; 211 if (text4 == "+") 212 { 213 jishu = jieguo; 214 jieguo += jishu; 215 216 textBox2.Text = jieguo.ToString(); 217 } 218 else if (text4 == "-") 219 { 220 jishu = jieguo; 221 jieguo -=jishu; 222 223 textBox2.Text = jieguo.ToString(); 224 } 225 else if (text4 == "*") 226 { 227 jishu = jieguo; 228 jieguo=jieguo*jishu; 229 230 textBox2.Text = jieguo.ToString(); 231 } 232 else if (text4 == "/") 233 { 234 if (jieguo != 0) 235 { 236 jishu = jieguo; 237 jieguo = jieguo / jishu; 238 239 textBox2.Text = jieguo.ToString(); 240 } 241 else 242 { 243 textBox2.Text = "除数不能为0"; 244 } 245 } 246 else if (text4 == "=") 247 { 248 jishu =Convert.ToDouble(textBox1.Text); 249 if (text5 == "+") 250 { 251 jieguo+=jishu ; 252 } 253 else if (text5 == "-") 254 { 255 jieguo-=jishu; 256 } 257 else if (text5 == "*") 258 { 259 jieguo *=jishu; 260 } 261 else if (text5 == "/") 262 { 263 if (jishu != 0) 264 { 265 jieguo /= jishu; 266 } 267 else 268 { 269 textBox2.Text = "除数不能为0"; 270 } 271 } 272 } 273 } 274 yunsuanfu = false; 275 276 } 277 //按键C 278 private void button8_Click(object sender, EventArgs e) 279 { 280 textBox2.Clear(); 281 textBox1.Clear(); 282 textBox3.Clear(); 283 textBox2.Text = "0"; 284 textBox1.Text = ""; 285 textBox3.Text = ""; 286 jieguo = 0; 287 jieguo1 = 0; 288 yunsuanfu = false; 289 text = ""; 290 text4 = ""; 291 text5 = ""; 292 } 293 //CE键 294 private void button7_Click(object sender, EventArgs e) 295 { 296 297 textBox2.Clear(); 298 textBox2.Text = "0"; 299 } 300 //退格键 301 private void button6_Click(object sender, EventArgs e) 302 { 303 if (textBox2.Text.Length > 1) 304 { 305 textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1); 306 } 307 else if (textBox2.Text.Length == 1) 308 { 309 textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1); 310 textBox2.Text = "0"; 311 } 312 else 313 { 314 textBox2.Text = "0"; 315 } 316 } 317 //主屏幕字体随个数变化而变化 318 private void textBox2_TextChanged(object sender, EventArgs e) 319 { 320 if (textBox2.Text.Length < 8) 321 { 322 this.textBox2.Font = new System.Drawing.Font("宋体", 21.75f); 323 } 324 if (textBox2.Text.Length > 8) 325 { 326 this.textBox2.Font = new System.Drawing.Font("宋体", 15f); 327 } 328 if (textBox2.Text.Length > 12) 329 { 330 this.textBox2.Font = new System.Drawing.Font("宋体", 13f); 331 } 332 if (textBox2.Text.Length > 16) 333 { 334 this.textBox2.Font = new System.Drawing.Font("宋体", 11f); 335 } 336 }
winform制作简单计算器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。