In this part we are going to write some code. I will try to explain everything step by step.
Because we are creating a simple calculator, we are only interested in operations such as 7*3=21
and we are not considering the PEMDAS rule. so the answer to 4+3*4 will be 28 instead of 16. We'll create a more advanced calculator later, but now our goal is to learn the basics.
In the design mode press F7 to switch to the c# code.
First of all we are gonna declare some variables to use, write the declarations in the Form1 class.
Declarations:
string text; //The text that will show in the textBox double operand_1, operand_2, solution; char Operator;
From the design mode, double click on the Form, this will generate an event called Form1_Load, this event will launch when your form is loaded.
Generally we put the initial values of our variables in this event.
Form1_Load method
private void Form1_Load(object sender, EventArgs e) { operand_1 = operand_2 = solution = 0; text = "0"; Operator = '='; }
we will create two main methods, one for the numbers and the other for the operators (+, -, *, /, =).
Numbers method:
private void numbers_Click(object sender, EventArgs e) { Button button=(Button)sender; text += button.Text; while (text != "0" && text[0] == '0' && text[1] != '.') text = text.Substring(1); textBox1.Text = text; }
This code will execute only when the user click one of the 10 numeric buttons or the point button.
- In the first line I'm declaring a Button (which would not appear) I name it button. My goal here is just to know which button from the 11 was pressed, this would be the sender of type object.
- The second line is adding the Text property of the sender button (button) to the variable text.
- We don't want to allow user to write for example 0001 or maybe 00.1, the while loop will automatically convert them to the appropriate form 1 or 0.1
- Finally we show the text in textBox1 (the default name of the created TextBox)
Now we have to tell VS when to launch this method. Go back to design view, hold Ctrl key and select the 11 buttons, then click on Events tab in the properties window, look for the Click event, assign number_Click method to it.
Operators method:
Next, the number in the textBox (whether it was written or calculated) is ready for a new operation with the operator Operator, which is the Text property value of the clicked button from the operators buttons.
Don't forget to assign this method to the appropriate buttons from the design tab.
Testing
Now your application is ready to test, press F5 to start the debugger.
I suggest you to add some more features to this calculator.
If you have any questions leave a comment here or on my facebook page.
Operators method:
private void operators_Click(object sender, EventArgs e) { if (Operator!='=') { operand_2 = double.Parse(textBox1.Text); switch (Operator) { case '+': solution=operand_1+operand_2; break; case '-': solution=operand_1-operand_2; break; case '*': solution=operand_1*operand_2; break; case '/': solution=operand_1/operand_2; break; } Operator = '='; textBox1.Text = solution.ToString(); } operand_1 = double.Parse(textBox1.Text); Operator=char.Parse(((Button)sender).Text); text = "0"; }The first line will check if there's a previous operation to complete, if yes we complete it and set the Operator value back to '=' and print the result in the textBox.
Next, the number in the textBox (whether it was written or calculated) is ready for a new operation with the operator Operator, which is the Text property value of the clicked button from the operators buttons.
Don't forget to assign this method to the appropriate buttons from the design tab.
Testing
Now your application is ready to test, press F5 to start the debugger.
I suggest you to add some more features to this calculator.
If you have any questions leave a comment here or on my facebook page.