locked
Plus function on vb.net 2005 RRS feed

  • 問題

  • there are txtDisplay, btnOne, btnTwo, btnPlus, btnEqual.

     

    1. Press btnOne, "1" appears on txtDisplay

    2. Disappear "1" and Press btnPlus, "+" appears on txtDisplay

    3. Disappear "+" and Press btnTwo, "2" appears on txtDisplay

    4. Disappear "2" and Press btnEqual, "3" appears on txtDisplay

     

    here is my code:

    Code Snippet

    Public Class Form1
        Dim MyVariable As String
        Dim total1 As Integer = 0

     

        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 btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
            txtDisplay.Text = ""
            total1 = total1 + Val(txtDisplay.Text)
            txtDisplay.Text = ""
            txtDisplay.Text = total1
        End Sub

     

        Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click

            txtDisplay.Text = total1

        End Sub

     

    End Class

     

    My question is:

    how to handle btnEqual? I can't display the answer "3".

    2008年11月16日 10:26

解答

  • Your requirements seems to be can't fulfilled by the code you provided:

     

    Code Snippet

    Public Class Form1
        Dim MyVariable As String    'This variable will never used
        Dim total1 As Integer = 0

     

        Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
            txtDisplay.Text = txtDisplay.Text & btnOne.Text    'You are appending the Text in the txtDisplay box instead of just showing "1" on the text box
        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    'Same as above
        End Sub

     

    Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
            txtDisplay.Text = ""    'You have cleared the content in the txtDisplay hence the next statement won't have the expected result
            total1 = total1 + Val(txtDisplay.Text)
            txtDisplay.Text = ""
            txtDisplay.Text = total1
        End Sub

     

        Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click

            txtDisplay.Text = total1

        End Sub

     

    End Class

     

     

    Try to think about the logic of your program clearly first and then implement each requirement/state listed one by one.  I think you should eventually get the result you want.

     

    Sorry that I am not going to provide the complete code for you as again, this seems to be an assignment from school.  I am sure that the most important thing to you is to understand how to do programming instead of just completing one assignment.  Knowing how to design and complete a program will benefit you more.

    2008年11月16日 14:19

所有回覆

  • Your requirements seems to be can't fulfilled by the code you provided:

     

    Code Snippet

    Public Class Form1
        Dim MyVariable As String    'This variable will never used
        Dim total1 As Integer = 0

     

        Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
            txtDisplay.Text = txtDisplay.Text & btnOne.Text    'You are appending the Text in the txtDisplay box instead of just showing "1" on the text box
        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    'Same as above
        End Sub

     

    Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
            txtDisplay.Text = ""    'You have cleared the content in the txtDisplay hence the next statement won't have the expected result
            total1 = total1 + Val(txtDisplay.Text)
            txtDisplay.Text = ""
            txtDisplay.Text = total1
        End Sub

     

        Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click

            txtDisplay.Text = total1

        End Sub

     

    End Class

     

     

    Try to think about the logic of your program clearly first and then implement each requirement/state listed one by one.  I think you should eventually get the result you want.

     

    Sorry that I am not going to provide the complete code for you as again, this seems to be an assignment from school.  I am sure that the most important thing to you is to understand how to do programming instead of just completing one assignment.  Knowing how to design and complete a program will benefit you more.

    2008年11月16日 14:19
  • You haven't make use of the variable - MyVariable which is very important to store and sum up the values. Please take a look at this article for reference: http://www.homeandlearn.co.uk/net/nets1p17.html

    Regards,
    Colt
    2008年11月16日 16:17
  • Colt, he is using Total1 to store the sum.

    2008年11月18日 9:24
  • Hi Ken, 

    total1 is storing a value of an empty string as shown in his code instead:

    Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
            txtDisplay.Text = ""

            total1 = total1 + Val(txtDisplay.Text)


    Regards,
    Colt
    2008年11月19日 15:36