Results 1 to 8 of 8

Thread: Having problems building a calculater

  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.

  2. #2

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

    Re: Having problems building a calculater

    At the beginning I ment to say, I am going through the instructions, not I am going of the instructions.

  3. #3
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Having problems building a calculater

    If you meant to say something just click the EDIT button at the bottom of your post. I suggest you do that now and add [code] tags to all of your code so that it is easier to read.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  4. #4
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: I'm having trouble............

    Quote Originally Posted by wizz1 View Post
    yea, I know, but NO ONE is answering!!
    I answered, and you still haven't listened.

    Also this is not a 24/7 help center were you get help within the first minute of posting. We are all volunteers meaning if we don't want to, then no one has to help you.

    You posted your topic, now you have to wait. It could be days before someone posts, until then, don't double post!
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: I'm having trouble............

    The fact that noone has answered within 3 hours is not a reason to repost exactly the same thing. There's NEVER a reason to post exactly the same thing. If noone is answering then there's probably a reason, like perhaps noone who has viewed it so far has either the time or the inclination to read such a long post. Generally speaking, although not always, if you feel you need to post that much information then you're probably trying to tackle too much in one go. Posting relevant information is good but maybe you need to break your problem down and tackle it one bit at a time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Having problems building a calculater

    This part is just an example of how to code better.

    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.
    Notice how after that he says "Back to the Code", that is when you go back to programming your calculator.

    Back to our code.

    If we're going to be adding two numbers together...
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: I'm having trouble............

    I just realised that you've simply copied and pasted everything from that other site. Why would you need to do that when, if we want to read it, we can just follow the link you posted? Of course, it would help if you posted a link that works. You've succeeded in scaring people away from helping you by posting lots of irrelevant information. You need to post all the information we need and none that we don't.

    It should be fairly obvious that, if you want your calculator to work, then you'll have to implement the code they've provided. The idea is that they are showing you the principles involved with examples. You are supposed to do what they show you in your own project and then run it to see it in action. The Exercise is then a chance for you to write some code for yourself using the principles that they've demonstrated previously.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Having problems building a calculater

    Duplicate threads merged - please post each question (or variation of it) only once.

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