Today, we’re going to create a more advanced adder than what we made on Monday.
This adder is going to be more like a calculator, in that it will have a button for each number, and one textbox for displaying not only what the user is typing, but also the sum.
1. Open Visual C# Express and create a new Winforms project named “ComplexAdder”
2. On the form, add a TextBox control and name it “entryBox”
3. Add 12 buttons to the form. These buttons are going to represent digits, as well as the addition function and the equals function. Name them “oneButton”, “twoButton”, “threeButton” etc, and include “plusButton” and “equalsButton”. There should be one for each number from 0 to 9, along with + and =. Change the text on these buttons appropriately.
4. Arrange the buttons on the form the way you might see them on a calculator.
Once we have this far, we’ll go on to the following instructions:
5. Create click events for all of the number buttons
6. In each number button click event, change the entryBox Text to be whatever is in the entryBox Text plus the value of the button that was clicked
When naming a control or a variable, there are naming conventions that must be followed. Names cannot have spaces in them, and they cannot start with a number, though they can have numbers inside the name.
We talked about method signatures. For example:
private void UpdateNumber(int number)
In this example, “private” indicates the protection level (private or public). “void” indicates what the method will return. “UpdateNumber” is the name of the method. Everything between the parenthesis is the arguments for the method. In this case, the method takes one argument, an int called “number.”
We didn’t get all the way to a functioning calculator, but we made it fairly far.