标签:
简单计算器,暂时label控件的显示功能还不完善,不想了,就这样
界面设计:
显示计算公式:lblShow
显示按下的数字和结果:txtShow
0-9按钮:btnZero~btnNine
按钮点:btnPoint
加:btnAdd
减:btnMinus
乘:btnMultiply
除:btnDivided
回退:btnFallback
CE按钮:btnCE
C按钮:btnC
等于按钮:btnEqual
为防止误操作,把文本框的属性设置成不可手动修改:
原文本框高度太小,属性修改:
详细代码:
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 计算器 12 { 13 public partial class Counter : Form 14 { 15 public Counter() 16 { 17 InitializeComponent(); 18 } 19 //设置全局变量 20 //加减乘除运算符 21 string Operator; 22 //前数值 23 decimal FrontValue; 24 //后数值 25 decimal BehindValue; 26 //判断已有小数点 27 bool AlreadyPoint=false; 28 //判断已等于 29 bool AlreadyEqual = false; 30 31 private void Counter_Load(object sender, EventArgs e) 32 { 33 lblShow.Text = ""; 34 } 35 36 private void btnZero_Click(object sender, EventArgs e) 37 { 38 if (AlreadyEqual == true) 39 { 40 txtShow.Text = ""; 41 lblShow.Text = ""; 42 AlreadyEqual = false; 43 } 44 txtShow.Text += 0; 45 lblShow.Text += 0; 46 } 47 48 private void btnOne_Click(object sender, EventArgs e) 49 { 50 if (AlreadyEqual == true) 51 { 52 txtShow.Text = ""; 53 lblShow.Text = ""; 54 AlreadyEqual = false; 55 } 56 txtShow.Text += 1; 57 lblShow.Text += 1; 58 } 59 60 private void btnTwo_Click(object sender, EventArgs e) 61 { 62 if (AlreadyEqual == true) 63 { 64 txtShow.Text = ""; 65 lblShow.Text = ""; 66 AlreadyEqual = false; 67 } 68 txtShow.Text += 2; 69 lblShow.Text += 2; 70 } 71 72 private void btnThree_Click(object sender, EventArgs e) 73 { 74 if (AlreadyEqual == true) 75 { 76 txtShow.Text = ""; 77 lblShow.Text = ""; 78 AlreadyEqual = false; 79 } 80 txtShow.Text += 3; 81 lblShow.Text += 3; 82 } 83 84 private void btnFour_Click(object sender, EventArgs e) 85 { 86 if (AlreadyEqual == true) 87 { 88 txtShow.Text = ""; 89 lblShow.Text = ""; 90 AlreadyEqual = false; 91 } 92 txtShow.Text += 4; 93 lblShow.Text += 4; 94 } 95 96 private void btnFive_Click(object sender, EventArgs e) 97 { 98 if (AlreadyEqual == true) 99 { 100 txtShow.Text = ""; 101 lblShow.Text = ""; 102 AlreadyEqual = false; 103 } 104 txtShow.Text += 5; 105 lblShow.Text += 5; 106 } 107 108 private void btnSix_Click(object sender, EventArgs e) 109 { 110 if (AlreadyEqual == true) 111 { 112 txtShow.Text = ""; 113 lblShow.Text = ""; 114 AlreadyEqual = false; 115 } 116 txtShow.Text += 6; 117 lblShow.Text += 6; 118 } 119 120 private void btnSeven_Click(object sender, EventArgs e) 121 { 122 if (AlreadyEqual == true) 123 { 124 txtShow.Text = ""; 125 lblShow.Text = ""; 126 AlreadyEqual = false; 127 } 128 txtShow.Text += 7; 129 lblShow.Text += 7; 130 } 131 132 private void btnEight_Click(object sender, EventArgs e) 133 { 134 if (AlreadyEqual == true) 135 { 136 txtShow.Text = ""; 137 lblShow.Text = ""; 138 AlreadyEqual = false; 139 } 140 txtShow.Text += 8; 141 lblShow.Text += 8; 142 } 143 144 private void btnNine_Click(object sender, EventArgs e) 145 { 146 if (AlreadyEqual == true) 147 { 148 txtShow.Text = ""; 149 lblShow.Text = ""; 150 AlreadyEqual = false; 151 } 152 txtShow.Text += 9; 153 lblShow.Text += 9; 154 } 155 156 private void btnPoint_Click(object sender, EventArgs e) 157 { 158 if (AlreadyEqual == true) 159 { 160 txtShow.Text = ""; 161 } 162 if (txtShow.Text != "" && AlreadyPoint==false) 163 { 164 AlreadyPoint = true; 165 txtShow.Text += "."; 166 } 167 } 168 //加 169 private void btnAdd_Click(object sender, EventArgs e) 170 { 171 if (txtShow.Text != "") 172 { 173 FrontValue += decimal.Parse(txtShow.Text); 174 txtShow.Text = ""; 175 lblShow.Text += " + "; 176 Operator = "add"; 177 AlreadyPoint = false; 178 } 179 } 180 //减 181 private void btnMinus_Click(object sender, EventArgs e) 182 { 183 if (txtShow.Text != "") 184 { 185 FrontValue += decimal.Parse(txtShow.Text); 186 txtShow.Text = ""; 187 lblShow.Text += " - "; 188 Operator = "Minus"; 189 AlreadyPoint = false; 190 } 191 } 192 //乘 193 private void btnMultiply_Click(object sender, EventArgs e) 194 { 195 if (txtShow.Text != "") 196 { 197 FrontValue += decimal.Parse(txtShow.Text); 198 txtShow.Text = ""; 199 lblShow.Text += " * "; 200 Operator = "Multiply"; 201 AlreadyPoint = false; 202 } 203 } 204 //除 205 private void btnDivided_Click(object sender, EventArgs e) 206 { 207 if (txtShow.Text != "") 208 { 209 FrontValue += decimal.Parse(txtShow.Text); 210 txtShow.Text = ""; 211 lblShow.Text += " / "; 212 Operator = "Divided"; 213 AlreadyPoint = false; 214 } 215 } 216 //等于 217 private void btnEqual_Click(object sender, EventArgs e) 218 { 219 switch(Operator) 220 { 221 case "add": 222 BehindValue = FrontValue + decimal.Parse(txtShow.Text); 223 break; 224 case "Minus": 225 BehindValue = FrontValue - decimal.Parse(txtShow.Text); 226 break; 227 case "Multiply": 228 BehindValue = FrontValue * decimal.Parse(txtShow.Text); 229 break; 230 case "Divided": 231 BehindValue = FrontValue / decimal.Parse(txtShow.Text); 232 break; 233 } 234 txtShow.Text = BehindValue.ToString(); 235 FrontValue = 0; 236 AlreadyEqual = true; 237 AlreadyPoint = false; 238 lblShow.Text = txtShow.Text; 239 } 240 //CE清除当前数 241 private void btnCE_Click(object sender, EventArgs e) 242 { 243 txtShow.Text = ""; 244 //清除功能对lblShow缺失,没想到好方法 245 lblShow.Text = ""; 246 } 247 //C全清 248 private void btnC_Click(object sender, EventArgs e) 249 { 250 txtShow.Text = ""; 251 lblShow.Text = ""; 252 //加减乘除运算符 253 Operator=""; 254 //前数值 255 FrontValue=0; 256 //后数值 257 BehindValue=0; 258 //判断已有小数点 259 AlreadyPoint = false; 260 //判断已等于 261 AlreadyEqual = false; 262 } 263 //回退功能 264 private void btnFallback_Click(object sender, EventArgs e) 265 { 266 if (txtShow.Text != "") 267 { 268 txtShow.Text = txtShow.Text.Substring(0, txtShow.Text.Length-1); 269 lblShow.Text = lblShow.Text.Substring(0, lblShow.Text.Length - 1); 270 } 271 } 272 } 273 }
运行效果:
附源码下载:
标签:
原文地址:http://www.cnblogs.com/start-from-scratch/p/5437278.html