Write a simple calculator using the C # language:
The source code is as follows:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Namespace simple calculator
{
Public partial class Form1 : Form
{
Public Form1()
{
InitializeComponent();
}
Private void button1_Click(object sender, EventArgs e)//addition
{
String str1 = textBox1.Text;//str1 saves the contents of the first text box
String str2 = textBox2.Text;//str2 saves the contents of the second text box
Int i1, i2;
If (!int.TryParse(str1, out i1))// is equivalent to if (!int.TryParse(str1, out i1)==false), converts the first text box content string into integer data
{
MessageBox.Show("The first number is not a valid integer"); / / pop-up message dialog
Return; / / do not forget to return, just exit the function
}
If (int.TryParse(str2, out i2) == false)//converts the second text box content string to integer data
{
MessageBox.Show("The second number is not a valid integer"); / / pop-up message dialog
Return;
}
Int i3 = i1 + i2; / / perform operations
textBox3.Text = Convert.ToString(i3);// Equivalent to textBox3 = i3.ToString();
}
Private void button2_Click(object sender, EventArgs e)//Click to hide the text box
{
textBox1.Hide();//The first text box is hidden
textBox2.Hide();//The second text box is hidden
textBox3.Hide();//The third text box is hidden
textBox4.Hide();
textBox5.Hide();
textBox6.Hide();
textBox7.Hide();
textBox8.Hide();
textBox9.Hide();
textBox10.Hide();
textBox11.Hide();
textBox12.Hide();
}
Private void button3_Click(object sender, EventArgs e)//Click to display the text box
{
textBox1.Show();//The first text box is displayed
textBox2.Show();//The second text box is displayed
textBox3.Show();//The third text box is displayed
textBox4.Show();
textBox5.Show();
textBox6.Show();
textBox7.Show();
textBox8.Show();
textBox9.Show();
textBox10.Show();
textBox11.Show();
textBox12.Show();
}
Private void button4_Click(object sender, EventArgs e)//subtraction
{
String str3 = textBox4.Text;
String str4 = textBox5.Text;
Int i3, i4;
If (!int.TryParse(str3,out i3))
{
MessageBox.Show("The first number is not a legal integer");
Return;
}
If (!int.TryParse(str4,out i4))
{
MessageBox.Show("The second number is not legal data");
}
Int i5 = i3 -i4;
textBox6.Text = Convert.ToString(i5);
}
Private void button5_Click(object sender, EventArgs e)//multiplication
{
String str3 = textBox7.Text;
String str4 = textBox8.Text;
Int i3, i4;
If (!int.TryParse(str3, out i3))
{
MessageBox.Show("The first number is not a legal integer");
Return;
}
If (!int.TryParse(str4, out i4))
{
MessageBox.Show("The second number is not legal data");
}
Int i5 = i3 *i4;
textBox9.Text = Convert.ToString(i5);
}
Private void button6_Click(object sender, EventArgs e)//division
{
String str3 = textBox10.Text;
String str4 = textBox11.Text;
Int i3, i4;
If (!int.TryParse(str3, out i3))
{
MessageBox.Show("The first number is not a legal integer");
Return;
}
If (!int.TryParse(str4, out i4))
{
MessageBox.Show("The second number is not legal data");
}
Int i5 = i3 / i4;
textBox12.Text = Convert.ToString(i5);
}
}
}