Winform (4)-simple calculator and winform Calculator

Source: Internet
Author: User

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 + = ". ";}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.