- Back to Home »
- C# Tutorial »
- C# Calculator - The Plus Button
Posted by : senan
Friday, February 14, 2014
So we've got the numbers to appear in the text box when a button
is clicked. The next thing we need to do is grab that number and store it somewhere.
We'll then use this number when the equals button is clicked. (The equals button
will do the addition, not the plus button.)
The plus button, then, needs to grab the number from the text box. Because
it's text, we need to convert it to a number. We'll then store that number in
a variable. The only other line of code we'll need for the Plus button is to
clear the text box, ready for the second number.
The first number needs to be stored in a variable. We'll use the double type
of variable. That way, we can have really big numbers with a "point something"
at the end.
So that all the buttons in the programme can see this variable, it needs to
be set outside of any button code. So we can't do this:
private void btnOne_Click(object sender, EventArgs e)
{
{
double total1 = 0;
}
If you set up a variable inside of a button only this button will be able to
do anything with the variable. This is known as scope. Because we've set up
the variable inside of the button code, we've given it local scope: it can only
be seen inside of the curly brackets. To make the variable accessible to all
the buttons, we need to give it what's knows as global scope. This is fairly
easy - just set it up outside of any buttons. Like this:
double total1 = 0;
private void btnPlus_Click(object sender, EventArgs e)
{
{
}
Now the variable is outside of the curly brackets, making it available to all
the buttons on our form. So set up that variable in your own code.
For the btnPlus code itself, add the following two lines (in blue bold
below):
double total1 = 0;
private void btnPlus_Click(object sender, EventArgs e)
{
{
total1 = total1 + double.Parse( txtDisplay.Text );
txtDisplay.Clear();
txtDisplay.Clear();
}
All we're doing here is getting the text from the text box, converting it to
a double number, and then storing it in the total1 variable. Notice that we've
also done this:
total1 = total1 +
Just like we did for the number buttons, we need to keep whatever was in the
total1 variable. You need to do this in case you want to add more than
two numbers. If you didn't keep what was in the total1 variable, C# would "forget"
what was in it, and start afresh. This technique is so common in programming
that a shorthand way of doing this is usually implemented:
total1 += double.Parse(txtDisplay.Text);
So instead of repeating the variable name, you just use a plus symbol and an
equals symbol together ( += ). The above line does exactly the same thing as
this:
total1 = total1 + double.Parse(txtDisplay.Text);
But whichever way you choose to retain a value in a variable, all you're saying
is "Keep whatever is already in the variable, and add something else".
After storing the number from the text box, we need to clear it:
txtDisplay.Clear( );
Once the text box is cleared, a second number can be selected by clicking the
number buttons.
In the next part, you'll learn how to code for the Equals button.