To code a project in C#, it is recommended that you use Microsoft’s official IDE, Visual Studio.
Before starting work on this project, Visual Studio needs to be installed and a new Windows Form project needs to be created.
Next, a basic user interface needs to be designed.
To design the user interface in Visual Studio, objects can be dragged and dropped on the window once a Windows form project is created. A TextBox should be inserted to display the input and output, and buttons should be inserted to allow for input. To speed the process up, buttons can be copied and pasted using Ctrl+C
and Ctrl+V
.
The buttons need to be programmed to do what is required of them.
class Calculator { // To store current number being input string input = "0"; // To store first input number string num1 = "0"; // To store second number string num2 = "0"; // To store the operator string function = string.empty; // To initialize the program public Form1() { initializeComponent(); } // Handling numeric input public void button_Zero(object o, EventArgs e) { if (input != "0"){ // Updating input input = input + "0"; } else { input = "0"; } this.textBox.Text = ""; this.textBox.Text += input; } public void button_One(object o, EventArgs e) { if (input != "0"){ // Updating input input = input + "1"; } else { input = "1"; } this.textBox.Text = ""; this.textBox.Text += input; } public void button_Two(object o, EventArgs e) { if (input != "0"){ // Updating input input = input + "2"; } else { input = "2"; } this.textBox.Text = ""; this.textBox.Text += input; } public void button_Three(object o, EventArgs e) { if (input != "0"){ // Updating input input = input + "3"; } else { input = "3"; } this.textBox.Text = ""; this.textBox.Text += input; } public void button_Four(object o, EventArgs e) { if (input != "0"){ // Updating input input = input + "4"; } else { input = "4"; } this.textBox.Text = ""; this.textBox.Text += input; } public void button_Five(object o, EventArgs e) { if (input != "0"){ // Updating input input = input + "5"; } else { input = "5"; } this.textBox.Text = ""; this.textBox.Text += input; } public void button_Six(object o, EventArgs e) { if (input != "0"){ // Updating input input = input + "6"; } else { input = "6"; } this.textBox.Text = ""; this.textBox.Text += input; } public void button_Seven(object o, EventArgs e) { if (input != "0"){ // Updating input input = input + "7"; } else { input = "7"; } this.textBox.Text = ""; this.textBox.Text += input; } public void button_Eight(object o, EventArgs e) { if (input != "0"){ // Updating input input = input + "8"; } else { input = "8"; } this.textBox.Text = ""; this.textBox.Text += input; } public void button_Nine(object o, EventArgs e) { if (input != "0"){ // Updating input input = input + "9"; } else { input = "9"; } this.textBox.Text = ""; this.textBox.Text += input; } // Handling operator input public void button_Plus(object o, EventArgs e) { num1 = input; input = "0"; this.textBox.Text = ""; function = "+"; } public void button_Minus(object o, EventArgs e) { num1 = input; input = "0"; this.textBox.Text = ""; function = "-"; } public void button_Multiply(object o, EventArgs e) { num1 = input; input = "0"; this.textBox.Text = ""; function = "*"; } public void button_Divide(object o, EventArgs e) { num1 = input; input = "0"; this.textBox.Text = ""; function = "/"; } // Handling result public void button_Equal(object o, EventArgs e) { num2 = input; if (function != string.empty){ double n1, n2, result; try { // Convert to double data type double.TryParse(num1, n1); double.TryParse(num2, n2); // Checking operator and calculating output switch(function){ case "+": this.textBox.Text = ""; result = n1 + n2; this.textBox.Text = result.ToString(); break; case "-": this.textBox.Text = ""; result = n1 - n2; this.textBox.Text = result.ToString(); break; case "*": this.textBox.Text = ""; result = n1 * n2; this.textBox.Text = result.ToString(); break; case "/": this.textBox.Text = ""; result = n1 / n2; this.textBox.Text = result.ToString(); break; } } catch { // Handling Math Errors this.textBox.Text = "Math Error" input = ""; num1 = ""; num2 = ""; function = ""; } } } // Setting up clear button public void button_Clear(object o, EventArgs e) { input = ""; num1 = ""; num2 = ""; function = ""; this.textBox.Text = ""; } }
This program contains all of the event-handlers for button clicks on the calculator. It is a basic program that uses the globally declared variables input
, num1
, num2
, and function
. input
contains the number currently being input, num1
contains the first operand, num2
contains the second operand, and function
contains the operator.
When the operator is selected, the program transfers the contents of input
into num1
, inserts the appropriate operator into function
, and empties input
for the second operand.
When the =
button is pressed, the program copies the contents of input
into num2
and converts both num1
and num2
into doubles
. These converted values are stored in n1
and n2
and used to calculate and display the output. In the case of an error, the catch
statement will reset the values and display an error message.
If the C
button is pressed, all global variables are reset and the output box is emptied.
RELATED TAGS
View all Courses