|
C# Tutorial - A Simple Calculator – Visual Design
At this point, we have covered enough to build ourselves a calculator similar to the one that ships with Windows in Standard mode.
Create a new Windows Forms Application called WApp04.
Change the Text property of the form to "Simple Calculator".
Change the MaximizeBox property to False.
Add the following controls with the properties set to those shown. Note that the top control is a label and not a text box as it may first appear.
Labels
There are three labels in this application. The first label is where you expect the text box to be.
| Property |
Value |
| Name |
Result |
| Location |
12,10 |
| Size |
244,20 |
| Text |
0 |
| TextAlign |
MiddleRight |
| BackColor |
Window |
| BorderStyle |
FixedSingle |
The second label is the simple 'M' located next to the memory box.
| Property |
Value |
| Name |
label1 |
| Location |
9,42 |
| Size |
18,28 |
| Text |
M |
| TextAlign |
MiddleCenter |
The third label is used to display the contents of the memory.
| Property |
Value |
| Name |
MemText |
| Location |
26,42 |
| Size |
10,28 |
| Text |
0 |
| TextAlign |
MiddleLeft |
| BorderStyle |
Fixed3D |
Buttons
The twenty Six buttons are laid out with the following properties. Firstly we will deal with the buttons pertaining to memory.
| Property |
Value |
| Name |
buttonCE |
| Location |
140,42 |
| Size |
56,28 |
| Text |
CE |
| ForeColor |
Red |
| Property |
Value |
| Name |
buttonC |
| Location |
200,42 |
| Size |
56,28 |
| Text |
C |
| ForeColor |
Red |
| Property |
Value |
| Name |
buttonMC |
| Location |
12,82 |
| Size |
36,28 |
| Text |
MC |
| ForeColor |
Red |
| Property |
Value |
| Name |
buttonMR |
| Location |
12,114 |
| Size |
36,28 |
| Text |
MR |
| ForeColor |
Red |
| Property |
Value |
| Name |
buttonMS |
| Location |
12,146 |
| Size |
36,28 |
| Text |
MS |
| ForeColor |
Red |
| Property |
Value |
| Name |
buttonMP |
| Location |
12,178 |
| Size |
36,28 |
| Text |
M+ |
| ForeColor |
Red |
Now we can add the numeric keypad
| Property |
Value |
| Name |
button0 |
| Location |
60,178 |
| Size |
36,28 |
| Text |
0 |
| Property |
Value |
| Name |
button1 |
| Location |
60,146 |
| Size |
36,28 |
| Text |
1 |
| Property |
Value |
| Name |
button2 |
| Location |
100,146 |
| Size |
36,28 |
| Text |
2 |
| Property |
Value |
| Name |
button3 |
| Location |
140,146 |
| Size |
36,28 |
| Text |
3 |
| Property |
Value |
| Name |
button4 |
| Location |
60,114 |
| Size |
36,28 |
| Text |
4 |
| Property |
Value |
| Name |
button5 |
| Location |
100,114 |
| Size |
36,28 |
| Text |
5 |
| Property |
Value |
| Name |
button6 |
| Location |
140,114 |
| Size |
36,28 |
| Text |
6 |
| Property |
Value |
| Name |
button7 |
| Location |
60,82 |
| Size |
36,28 |
| Text |
7 |
| Property |
Value |
| Name |
button8 |
| Location |
100,82 |
| Size |
36,28 |
| Text |
8 |
| Property |
Value |
| Name |
button9 |
| Location |
140,82 |
| Size |
36,28 |
| Text |
9 |
Finally we add those buttons that will allow us to do some arithmetic.
| Property |
Value |
| Name |
buttonPM |
| Location |
100,78 |
| Size |
36,28 |
| Text |
+/- |
| Property |
Value |
| Name |
buttonDP |
| Location |
140,178 |
| Size |
36,28 |
| Text |
. |
| Property |
Value |
| Name |
buttonDivide |
| Location |
180,82 |
| Size |
36,28 |
| Text |
/ |
| Property |
Value |
| Name |
buttonMultiply |
| Location |
180,114 |
| Size |
36,28 |
| Text |
x |
| Property |
Value |
| Name |
buttonMinus |
| Location |
180,146 |
| Size |
36,28 |
| Text |
- |
| Property |
Value |
| Name |
buttonPlus |
| Location |
180,178 |
| Size |
36,28 |
| Text |
+ |
| Property |
Value |
| Name |
buttonSQRT |
| Location |
220,82 |
| Size |
36,28 |
| Text |
Sqrt |
| Property |
Value |
| Name |
buttonPercent |
| Location |
220,114 |
| Size |
36,28 |
| Text |
% |
| Property |
Value |
| Name |
buttonOneOver |
| Location |
220,146 |
| Size |
36,28 |
| Text |
1/x |
| Property |
Value |
| Name |
buttonEquals |
| Location |
220,178 |
| Size |
36,28 |
| Text |
= |
All you have to do now is size the form appropriately and we have the look of a simple calculator, but no functionality behind it.
© Publicjoe, 2008
|