Because I searched the Internet today c# Written calculator , Most of the findings are too cumbersome , A lot of unnecessary and not easy to understand things to write this blog
1. First create a new one windows Items applied to the form . Executive document - newly build - project -windows Form Application
2. Drag one out of the toolbox textbox For input and display , Pull it out again 21 individual button The button is used as the key of the calculator , stay textbox There's another one below lable control ( I changed the property to a space, so I can't see it ), Change the button text attribute
3. Double click the number button to enter the code interface ( The number is only one event , The operator also uses an event , Every other button needs to double-click to add an event )
4. The code is ready , Just double click the button to enter the code interface , It's on the line and it's stuck ( Note that all numbers are used as an event , They all have labels , You can select the button , Then click the event in the properties ( lightning icon ) see click Events of )
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 Calculator
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
// Defining variables
char oper; double num1; double num2; double result = 0; double memory=0.0;
private void Button9_Click(object sender, EventArgs e)// Function realization of digital button { Button a =
(Button)sender;// Determine which button was pressed if (textBox1.Text == "0") { textBox1.Text = a.Text;
} else textBox1.Text += a.Text; } private void Button16_Click(object sender,
EventArgs e)// Function realization of operator button { if (textBox1.Text != "") { num1 =
double.Parse(textBox1.Text); oper = char.Parse(((Button)sender).Text);
textBox1.Text = ""; } } private void Button15_Click(object sender, EventArgs
e)//C Function realization of button { textBox1.Text = ""; textBox1.Focus(); num1 = 0; num2 = 0; oper =
' '; } private void Button14_Click(object sender, EventArgs e)// Function realization of result button { if
(textBox1.Text != "") { num2 = double.Parse(textBox1.Text); switch (oper) {
case '+': result = num1 + num2; break; case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break; case '÷': if(num2=0) {
MessageBox.Show("0 Cannot be divisor !", " Tips ", MessageBoxButtons.OK,
MessageBoxIcon.Information); break; } result = num1 / num2; break;
textBox1.Text = result.ToString(); } } private void Button17_Click(object
sender, EventArgs e)// Function realization of decimal point button { if (textBox1.Text != "") { textBox1.Text +=
"."; } else { textBox1.Text = "0."; } } private void Button18_Click(object
sender, EventArgs e)//M+ Function realization of button { if(textBox1.Text!="") { label1.Text = "M";
memory += double.Parse(textBox1.Text); textBox1.Text = " "; } } private void
Button20_Click(object sender, EventArgs e)//MR Function realization of button { textBox1.Text =
memory.ToString(); } private void Button21_Click(object sender, EventArgs
e)//MC Function realization of button { label1.Text = ""; memory = 0; } private void
Button19_Click(object sender, EventArgs e)//M- Function realization of button { if (textBox1.Text !=
"") { label1.Text = "M"; memory -= double.Parse(textBox1.Text); textBox1.Text =
" "; } } }
}
Technology