Winform (4)-simple calculator and winform Calculator
:
Code area:
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace WindowsFormsApplication2 {public partial class Form1: Form {// What button was last clicked by storage, 0 indicates that nothing was clicked, and 1 indicates that the digit button was clicked, 2 indicates that the private int prev = 0 is clicked. // The calculation result is private decimal zj = 0. // The operator private string prevysf = "+" that was last followed is recorded "; public Form1 () {InitializeComponent () ;}// number key Button private void button8_Click (object sender, EventArgs e) {// convert the event source to Button btn = sender as Button; // Replace (if the content in the text box below is 0 or the operator is clicked last time) if (prev = 2 | txtbottom. text = "0") {txtbottom. text = btn. text;} // append (if the content of the Text box below is not 0 and the operator is not clicked last time) else {txtbottom. text + = btn. text;} // click the number Button prev = 1;} // operator Button private void button17_Click (object sender, EventArgs e) {Button btn = sender as Button; // if (prev = 1) {txttop. text + = txtbottom. text + btn. text; switch (prevysf) {case "+": zj = zj + Convert. toDecimal (txtbottom. text); break; case "-": zj = zj-Convert. toDecimal (txtbottom. text); break; case "*": zj = zj * Convert. toDecimal (txtbottom. text); break; case "/": zj = zj/Convert. toDecimal (txtbottom. text); break;} txtbottom. text = zj. toString ();} // The operator else {string s = txttop. text; s = s. substring (0, s. length-1); s = s + btn. text; txttop. text = s;} // click the operator prev = 2; // record the operator prevysf = btn. text;} // The private void button19_Click (object sender, EventArgs e) {txttop. text = ""; txtbottom. text = "0"; prev = 0; zj = 0; prevysf = "+";} private void button20_Click (object sender, EventArgs e) {txtbottom. text = "0";} // equals sign Button private void button4_Click (object sender, EventArgs e) {Button btn = sender as Button; txttop. text + = txtbottom. text + btn. text; switch (prevysf) {case "+": zj = zj + Convert. toDecimal (txtbottom. text); break; case "-": zj = zj-Convert. toDecimal (txtbottom. text); break; case "*": zj = zj * Convert. toDecimal (txtbottom. text); break; case "/": zj = zj/Convert. toDecimal (txtbottom. text); break;} txtbottom. text = zj. toString (); txttop. text = ""; zj = 0;} // point private void button3_Click (object sender, EventArgs e) {txtbottom. text + = ". ";}}}