Thursday, February 19, 2009

Building the "Calculator"

One of the things I did when building the calculator in class during the week (Calculator program blog) was to copy and paste. I know this seems like a fairly obvious tactic, but it can cause problems if you forget to change something.

When using similar pieces of code as we do in the calculator program, it's easy to copy and paste the code into each section as required. For example, you can take this code:
Dim intAddition1 As Integer = txtAdd1.Text
Dim intAddition2 As Integer = txtAdd2.Text
Dim intAdditionResult As Integer = intAddition1 + intAddition2
lblAddResult.Text = intAdditionResult.ToString

which is used to set up the addition part of the calculator and copy it in to the next event, which in our example is the multiplication event.

Once it has been copied, change "addition" to "multiplication" and "add" to "multiply" and you have your code ready to go for the next part of the program. Repeat these steps for each event as required and it will cut a lot of time off your task.

Keep in mind when doing this however that if you miss something, it will wreak havoc with your program, so there are some issues with doing things in this way.

Something else I discovered was how to align elements in a form. Using the calculator example again, you design your interface with all the labels, text boxes and buttons roughly where they should be. Then complete the following steps:
  1. Click the top label, then press and hold shift and click the remaining labels. It will show the first selected item with with white resizing squares and the subsequent items with black squares.
  2. Go to the menu bar and in the Format tab hover over the Align sub-menu and in the swing out menu, click on Lefts. This will align all of the items to the first selected item on their left hand sides.
  3. Next, select the first text box in each section as you did with the labels and format them the same way. (Do this with each item, including the symbols.)
  4. Go back to the first text box in the first section and select it again.
  5. This time, hold shift and click each item you want to align on the horizontal axis. (Again, include the symbols.)
  6. Go to the Format tab again and in the Align sub-menu, click on Middles. Repeat this step for each section and you should now have your interface beautifully aligned.
I will post the screen shots that along with this set of instructions below:

Step 1, Select the items.














Step 2, align the items.









Step3, do it with the text boxes (and each of the other items).











Step 4, select the first text box and then each item horizontally.











Step 5, align the items. Repeat with each group of items.









So there, you have it. It's pretty basic I know, but it's new knowledge to me so I find it exciting at least. If this has been at all helpful, please leave a comment and let me know. Alternatively, if you have a more efficient way of doing things and would like to share, please feel free to do that too.

Edit to previous post:

You will notice in the code above:

Dim intAddition1 As Integer = txtAdd1.Text etc

that it uses the data type Integer. The problem with this in a calculator is that, even if you enter whole numbers, the calculation may need to output fractional numbers. For example, the division equation 42/9 output an answer of 5 using the code as it was. What I did to fix this was change the Integer data type to the Decimal data type. It also required changing my name prefixes from int to dec and making sure that any references to integers in the code were also changed to decimal.

No comments:

Post a Comment