Results 1 to 9 of 9

Thread: having a bit of trouble with a new program

  1. #1

    Thread Starter
    Registered User
    Join Date
    Sep 2009
    Location
    England
    Posts
    112

    Question having a bit of trouble with a new program

    back ground- ive only been learning VB.net for about a week and am using a tutorial www.homeandlearn.co.uk.

    i am at the tutorial about the calculator and have hit a snag- my problem is that i have to do the equals button but dont know how to

    heres the sub of code

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

    Total2 = Total1 + (txtDisplay.Text)
    txtDisplay.Text = Total2
    txtDisplay.Clear()
    End Sub


    i am also having trouble with the add button- here is the code for that

    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



    any help would be greatly appreciated thanks in advance


    stuart

  2. #2
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    474

    Re: having a bit of trouble with a new program

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Total1 As Integer = 10
            Total1 = Total1 + CInt(TextBox1.Text)
            MsgBox(Total1.ToString)
        End Sub

  3. #3

    Thread Starter
    Registered User
    Join Date
    Sep 2009
    Location
    England
    Posts
    112

    Re: having a bit of trouble with a new program

    sorry but that didnt help, ill post the whole of my code so it may be possible to help more (by putting in the names of the boxes/buttons)

    Public Class Form1
    Dim Total1 As Single
    Dim Total2 As Single

    Private Sub btnZero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZero.Click
    txtDisplay.Text = txtDisplay.Text & btnZero.Text
    End Sub

    Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
    txtDisplay.Text = txtDisplay.Text & btnOne.Text
    End Sub

    Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
    txtDisplay.Text = txtDisplay.Text & btnTwo.Text
    End Sub

    Private Sub btnThree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThree.Click
    txtDisplay.Text = txtDisplay.Text & btnThree.Text
    End Sub

    Private Sub btnFour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFour.Click
    txtDisplay.Text = txtDisplay.Text & btnFour.Text
    End Sub

    Private Sub btnFive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFive.Click
    txtDisplay.Text = txtDisplay.Text & btnFive.Text
    End Sub

    Private Sub btnSix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSix.Click
    txtDisplay.Text = txtDisplay.Text & btnSix.Text
    End Sub

    Private Sub btnSeven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeven.Click
    txtDisplay.Text = txtDisplay.Text & btnSeven.Text
    End Sub

    Private Sub btnEight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEight.Click
    txtDisplay.Text = txtDisplay.Text & btnEight.Text
    End Sub

    Private Sub btnNine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNine.Click
    txtDisplay.Text = txtDisplay.Text & btnNine.Text
    End Sub

    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

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    txtDisplay.Text = ""
    txtDisplay.Clear()

    End Sub

    Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
    Total2 = Total1 + (txtDisplay.Text)
    txtDisplay.Text = Total2
    End Sub
    End Class


    thanks

  4. #4
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    474

    Re: having a bit of trouble with a new program

    I m not getting ur proble.Explain it.

  5. #5
    Frenzied Member Campion's Avatar
    Join Date
    Jul 2007
    Location
    UT
    Posts
    1,098

    Re: having a bit of trouble with a new program

    Disregard. I misread the + function.
    Last edited by Campion; Oct 9th, 2009 at 08:51 AM.
    From my burrow, 2 feet under.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: having a bit of trouble with a new program

    So what is it doing that you don't think it should be doing?

    By the way, you might as well learn good habits right from the start, so go to Project|Properties and turn Option Strict ON. This will result in a bunch of errors, but fixing them will make your code faster and avoid some bugs, potentially even the one you are dealing with here.

    Another point is that Val may not be your best alternative. There are times when it is just the easiest way to do things, and this might be one of those times, but consider using Single.TryParse, instead. It takes more code, but is also more robust and avoids some little oddities about Val.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Registered User
    Join Date
    Sep 2009
    Location
    England
    Posts
    112

    Re: having a bit of trouble with a new program

    the problem is that i was doing the tutorial and got to thins part

    Write 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.


    When you're finished, you should have a simple calculator that can add up integers. In the next section, we'll take a look at Conditional Logic, and why it's so important for your programming skills.



    and i dont know that to do for the three lines of code, i think i have done the first line right but not sure

  8. #8

    Thread Starter
    Registered User
    Join Date
    Sep 2009
    Location
    England
    Posts
    112

    Re: having a bit of trouble with a new program

    the code is working fine untill i press the equals button, it adds up but it stores total1 and adds everything to it- i have a clear button but that doesnt get rid of it

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: having a bit of trouble with a new program

    Right, after you put Total2 into wherever, add the line:

    Total1 = 0

    or else it will simply retain that Total1 value forever.
    My usual boring signature: Nothing

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