Results 1 to 8 of 8

Thread: Having problems building a calculater

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Location
    Australia
    Posts
    12

    Exclamation Having problems building a calculater

    I'm trying to build a calculater and I'm not sure what to do now. I am going through the instruction from http://www.homeandlearn.co.uk/net/nets1p19.htmlgetting. I'm not sure if I am ment to write this code, or is it an example of what you might want to write?

    Before the number vanishes, we can store it in a variable. The variable we're going to be storing the number in is one of those variable we set up at the top of the code. It's this one:

    Dim total1 As IntegerWe've already seen how to retain a value from a textbox and add it to something else:

    txtDisplay.Text = txtDisplay.Text & btnZero.Text
    Here, we kept the value that was already in the textbox and joined it to the Text property of the button.

    We can do something similar if we want to retain a value that is already in a variable. Examine this:

    variable1 = variable1 + 1The "= variable1 + 1" part just says "Remember what is in the variable variable1, and then add 1 to it. So if variable1 contain the number 3, what would variable1 now hold after that bit of code is executed? The whole code might be this

    variable1 = 3
    variable1 = variable1 + 1
    (If you don't know the answer to that, please send an email and ask for some further clarification on the subject.)

    The above is known in programming terms as "Incrementing a variable". There is even a shorthand you can use:

    variable1 += 1
    This says "variable1 equals variable1 plus 1". Or "Add 1 to variable1". You can also use the other mathematical operators with the same shorthand notation:



    This new code says "Multiply whatever is inside of variable1 by 3".

    The shorthand notation can be tricky to read (and to get used to), so we won't use it much. But it's there is you want it.


    Back to our code.

    If we're going to be adding two numbers together, we need Visual Basic to remember the first number. Because when we click the equals sign, that first number will be added to the second number. So if we haven't asked VB to remember the first number, it won't be able to add up.

    The variable we're going to be storing the first number in is total1. We could just say this:

    total1 = txtDisplay.Text

    Then whatever is in the textbox will get stored in the variable total1.

    Except we want VB to remember our numbers. Because we might want to add three or more numbers together: 1 + 2 + 3 + 4. If we don't keep a running total, there's no way for our programme to remember what has gone before, it will just erase whatever is in total1 and then start again.

    So just like the code above (varaible1 = variable1 + 1), we need to "tell" our programme to remember what was in the variable. We do it like this:

    total1 = total1 + Val(txtDisplay.Text)That Val( ) part just makes sure that a number in a textbox is kept as a number, and not as text. It's short for Value. The important part is the total1 + txtDisplay.Text. We're saying "The variable total1 contains whatever is in total1 added to the number in the textbox." An example might clear things up. Suppose we were adding 5 + 9. Now, suppose total1 already contained the number 5, and that number 9 was in the textbox. It would work like this:

    Finally, we need to erase the number in the textbox. To erase something from a textbox, just set its Text property to a blank string. We do this with two double quotation marks. Like this:

    txtDisplay.Text = ""
    That tiny bit of code will erase the Text a textbox. Another way to erase text from a textbox is to use the Clear method. After you typed a full stop, you probably saw the drop down list of Properties and Methods. Scroll up to the top, and locate the word Clear. Double click "Clear" and the drop down list will close. Hit the return key and VB adds two round brackets to your code:

    txtDisplay.Clear( )
    Notice that we're not setting the textbox to equal anything. We're using something called a Method. You can tell it's a Method because there's a purple block icon next to the word. A Method is a built-in bit of code that VB knows how to execute. In other words, it knows how to clear text from a textbox. You'll learn more about Methods later.

    So the whole code for our Button called btnPlus is this:

    Private Sub btnPlus_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnPlus.Click

    total1 = total1 + Val(txtDisplay.Text)txtDisplay.Clear()

    End Sub

    Add that code to your Plus button. All we've done with that code is to store numbers into our variable total1 and then erase whatever was in the textbox.

    We now need to add the numbers up.


    ExerciseWrite the code for the equals button. There's only three lines in total, and here's a little help.

    You need to use the other variable that was set up at the top of the coding window, total2. The variable total1 will be added to whatever is total2

    The first line of code will be this

    total2 = total1 + (something missing here)Your job is to supply the missing code. In other words, replace "(something missing here)"

    Remember that total1 contains the first number to be added. And you know where that came from. The only thing left to do is to add the second number.

    For the second line of code, you need to transfer the total2 variable to the textbox.


    For the third line of code, all you are doing is resetting the variable total1 to zero. That's because after the equals button has been pressed, we have finished adding up. If you wanted to do some more adding up, that total1 will still hold the value from the last time. If you reset it to zero, you can start adding some new numbers.

    The only thing left to do is to write a single line of code for the Clear button. All you are doing there is erasing text from a textbox. Which you already know how to do.

    Am I ment to do ALL of that? Or, should I only do the instructions from the Exercise part??

    Thanks,
    Last edited by wizz1; Jun 3rd, 2009 at 11:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width