Calculator
Open Visual Studio
Create a new project
Select Windows form app (.NET Framework)
Name the project as Calculator
Now Lets start to build our project.................
Change title
In the Right side of the window you can see the Properties box. Go to the properties and change the Text Form1 to Calculator
Change Icon
Scroll down the properties
Open the image.
Adding and customizing buttons
Go to toolbar in the left side of your Visual Studio Window
Drag and drop Button from there.
Resize the button, Go to properties and select size.
I resize the button to 35X35(You can resize it as you wish)
Change the buton text and make the font bold from the properties
Arrange more buttons as shown in below picture
Then change the texts
Now we need a Text box
change the textbox name in properties
I changed it into result_textbox
Now we want the text box filled with the number we clicked.
First click on a button, here I clicked on '0'
Select the Events option in properties.
Change click event into 'button_click'.Then double click on it.Now we need to enter the event code here.our code is
result_textBox.Text = result_textBox.Text + "0";
This same event is repeated for the other numbers and '.' buttons so copy the click event we just written and paste it on all the numbers and '.'(dot).
Now we need to change the button_click code a little because in the case of '0' button we given the text "0" but in the cases of other numbers we need to give the corresponding texts.
Button button = (Button)sender; result_textBox.Text = result_textBox.Text + button.Text;
just check the program by running it.
We know the calculator text align is from right. here it is left by default so change it in the properties.
In the text box we need "0" as default.
when a number is clicked the '0' must be cleared.So we can make a change in the code.
if (result_textBox.Text=="0")
result_textBox.Text.clear();
Now write the code for operator buttons (+ , - , * , /).
Operator_click is the event name I given to the event.
Now we go to the code section
First we need two variables declared in the starting of the code.
Double resultValue=0;
string operationPerformed="";
Our operator_click code is
private void operator_click(object sender, EventArgs e) { Button button = (Button)sender; operationPerformed = button.Text; resultValue = Double.Parse(result_textBox.Text); }
Now we can give the code for equals button. I give the name "equals_click" for the event.
The code for the equals_click is,
private void equals_click(object sender, EventArgs e) { if (operationPerformed == "+") { resultValue += Double.Parse(result_textBox.Text); } else if (operationPerformed == "-") { resultValue -= Double.Parse(result_textBox.Text); } else if (operationPerformed == "*") { resultValue *= Double.Parse(result_textBox.Text); } else if (operationPerformed == "/") { resultValue /= Double.Parse(result_textBox.Text); } else { resultValue = Double.Parse(result_textBox.Text); } result_textBox.Text = resultValue.ToString(); }
In the above code we are using if else statement to identify the operator.
At last we need the CE and C buttons to be coded. The C is used to clear all and CE is used to clear the current entry from the textbox. For that we use the below code.
For clear_click(C):
private void clear_click(object sender, EventArgs e) { result_textBox.Text="0"; resultValue = 0; }
For clearEntry_click(CE):
private void clear_click(object sender, EventArgs e) { result_textBox.Text="0"; }