Help with inputbox values?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tragic54
    New Member
    • Feb 2008
    • 20

    Help with inputbox values?

    Hello im new to visual basic, i'm on 2005 and need a little help with a project im doing. Once this code is executed an input box pops up and asks for a football score, once the score is entered it is displayed in a listbox like this.

    Points Scored 6 Total Points 6

    Now the problem is when i enter the second score from the next inputbox i'm getting the problem.

    Points Scored 6 Total Points 6
    Points Scored 3 Total Points 6
    etc.

    What i need is the Total Points Column to add the values as the input is entered, but the first Score in total points needs to remain the same.

    This is an example of what i need.

    Points Scored 6 Total Points 6
    Points Scored 3 Total Points 9
    Points Scored 6 Total Points 15

    The loop is ran until the user clicks the cancel button. I dont know exactly what i need to do to make this happen, but if someone could help pointing me in the right direction it would be appreciated. Heres the full code.

    Code:
    Option Strict On
    Option Explicit On
    
    Public Class frmScoreboard
    
        Private Sub mnuEnterScoreItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEnterScoreItem.Click
    
            'Declare and initialize variables
            Dim strPointsScored As String
            Dim decPointsScored As Decimal
            Dim strTotalScore2 As String
            Dim decTotalScore2 As Decimal
            Dim strTotalScore As String
            Dim decTotalScore As Decimal
            Dim decFinalScore As Decimal = 0D
            Dim strInputBoxMessage As String = "Enter score #"
            Dim strInputBoxHeading As String = "Points Scored"
            Dim strNormalBoxMessage As String = "Enter score #"
            Dim strNonNumericError As String = "Please enter a numeric value for score #"
            Dim strNegativeNumberError As String = "Please enter a positive value for score #"
    
            'Declare and initialize loop variables
            Dim intNumberOfEntries As Integer = 1
    
            strPointsScored = InputBox(strInputBoxMessage _
            & intNumberOfEntries, strInputBoxHeading, " ")
    
            strTotalScore = strPointsScored
            strTotalScore2 = strTotalScore
    
            Do Until (strPointsScored = "")
                If IsNumeric(strPointsScored) Then
                    decPointsScored = Convert.ToDecimal(strPointsScored)
                    decTotalScore = Convert.ToDecimal(strTotalScore)
                    decTotalScore2 = Convert.ToDecimal(strTotalScore2)
    
                    If decPointsScored > 0 Then
                        Me.lstScores.Items.Add("Points scored " & decPointsScored & _
                        "Total Points " & decTotalScore2)
                        decFinalScore = decPointsScored + decTotalScore2
                        intNumberOfEntries += 1
                        strInputBoxMessage = strNormalBoxMessage
                        decTotalScore2 += Me.lstScores.Items.Add(decTotalScore)
                    Else
                        strInputBoxMessage = strNegativeNumberError
                    End If
    
                Else
                    strInputBoxMessage = strNonNumericError
                End If
    
                If intNumberOfEntries > 0 Then
                    strPointsScored = InputBox(strInputBoxMessage _
                    & intNumberOfEntries, strInputBoxHeading)
    
                End If
    
            Loop
    
    
            Me.lblFinalScore.Visible = True
    
            If intNumberOfEntries > 1 Then
    
                Me.lblFinalScore.Text = "The Final Score is " & _
                decFinalScore.ToString("F1")
            Else
                Me.lblFinalScore.Text = "No score entered"
    
            End If
    
    
       End Sub
    End Class
  • jagged
    New Member
    • Feb 2008
    • 23

    #2
    On Line 35, change decTotalScore2 = Convert.ToDecim al(strTotalScor e2) to
    decTotalScore2 += Convert.ToDecim al(strTotalScor e2)

    so it keeps adding the last entered scored to the total score.

    I think Line 43 (decTotalScore2 += Me.lstScores.It ems.Add(decTota lScore) ) is useless. Here, you're adding the return value (if there is any) of the Add method to decTotalScore2. I dont think that's what you want.

    You can really do with less variables. For example, decTotalScore = decTotalScore2 = decFinalScore. Aside from messages / errors, you only really need two variables here (well, you can really get away with just one), intScoreEntered and intTotalScore (if you can't or shouldn't enter a fraction of a score, then use an integer). One line 25-28, you're saving the same info three times, and then converting them all to decimals a few lines later. Not that's necessary a bad thing, it's just hard to see what variable stores what. Trimming the fat will make your code a lot easier to debug and read.

    Comment

    • tragic54
      New Member
      • Feb 2008
      • 20

      #3
      Yeah I agree, i was just messing with quite a few variables trying to get it to add up... but yeah i trimmed it. The only problem is i'm trying to add the Total points in the right column based on the values entered in the input boxes.

      example this is what im getting.

      Points scored 20 Total Points 20
      Points scored 30 Total Points 40
      Points scored 50 Total Points 60
      Points scored 50 Total Points 80

      when it needs to be.

      Points scored 20 Total Points 20
      Points scored 30 Total Points 50
      Points scored 50 Total Points 100
      Points scored 50 Total Points 150

      Anyone have any idea what i'm doing wrong here?

      Code:
      Option Strict On
      Option Explicit On
      
      Public Class frmScoreboard
      
          Private Sub mnuEnterScoreItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEnterScoreItem.Click
      
              'Declare and initialize variables
              Dim strPointsScored As String
              Dim decPointsScored As Decimal
              Dim strTotalScore2 As String
              Dim decTotalScore2 As Decimal
              Dim decFinalScore As Decimal = 0D
              Dim strInputBoxMessage As String = "Enter score #"
              Dim strInputBoxHeading As String = "Points Scored"
              Dim strNormalBoxMessage As String = "Enter score #"
              Dim strNonNumericError As String = "Please enter a numeric value for score #"
              Dim strNegativeNumberError As String = "Please enter a positive value for score #"
      
              'Declare and initialize loop variables
              Dim intNumberOfEntries As Integer = 1
      
              strPointsScored = InputBox(strInputBoxMessage _
              & intNumberOfEntries, strInputBoxHeading, " ")
      
              strTotalScore2 = strPointsScored
      
              Do Until (strPointsScored = "")
                  If IsNumeric(strPointsScored) Then
                      decPointsScored = Convert.ToDecimal(strPointsScored)
                      decTotalScore2 += Convert.ToDecimal(strTotalScore2)
      
                      If decPointsScored > 0 Then
                          Me.lstScores.Items.Add("Points scored " & decPointsScored & _
                          "Total Points " & decTotalScore2)
                          decFinalScore = decPointsScored + decTotalScore2
                          intNumberOfEntries += 1
                          strInputBoxMessage = strNormalBoxMessage
                      Else
                          strInputBoxMessage = strNegativeNumberError
                      End If
      
                  Else
                      strInputBoxMessage = strNonNumericError
                  End If
      
                  If intNumberOfEntries > 0 Then
                      strPointsScored = InputBox(strInputBoxMessage _
                      & intNumberOfEntries, strInputBoxHeading)
      
                  End If
      
              Loop
      
      
              Me.lblFinalScore.Visible = True
      
              If intNumberOfEntries > 1 Then
      
                  Me.lblFinalScore.Text = "The Final Score is " & _
                  decFinalScore.ToString("F1")
              Else
                  Me.lblFinalScore.Text = "No score entered"
      
              End If
      
      
          End Sub
      End Class

      Comment

      • jagged
        New Member
        • Feb 2008
        • 23

        #4
        Code:
        'Declare and initialize variables
                Dim strPointsScored As String
                Dim decPointsScored As Decimal
                Dim decFinalScore As Decimal
        
                ' Messages
                Dim strInputBoxMessage As String = "Enter score #"
                Dim strInputBoxHeading As String = "Points Scored"
                Dim strNormalBoxMessage As String = "Enter score #"
                Dim strNonNumericError As String = "Please enter a numeric value for score #"
                Dim strNegativeNumberError As String = "Please enter a positive value for score #"
        
                'Declare and initialize loop variables
                Dim intNumberOfEntries As Integer = 1
        
                strPointsScored = InputBox(strInputBoxMessage & intNumberOfEntries, strInputBoxHeading, " ")
        
                Do Until (strPointsScored = "")
        
                    If IsNumeric(strPointsScored) Then
                        decPointsScored = Convert.ToDecimal(strPointsScored)
        
                        If decPointsScored > 0 Then
        
                            decFinalScore += decPointsScored
        
                            Me.lstscores.Items.Add("Points scored " & decPointsScored & _
                            "Total Points " & decFinalScore)
        
                            intNumberOfEntries += 1
        
                            strInputBoxMessage = strNormalBoxMessage
        
                        Else
                            ' What happens if scored = 0 ?
                            strInputBoxMessage = strNegativeNumberError
                        End If
        
                    Else
                        strInputBoxMessage = strNonNumericError
                    End If
        
                    ' Can intNumberOfEntries ever equal 0 ?
                    ' Var is init at 1
                    'If intNumberOfEntries > 0 Then
                    '    strPointsScored = InputBox(strInputBoxMessage _
                    '    & intNumberOfEntries, strInputBoxHeading)
                    'End If
        
                    strPointsScored = InputBox(strInputBoxMessage & intNumberOfEntries, strInputBoxHeading)
        
                Loop
        
        
                Me.lblfinalscore.Visible = True
        
                If intNumberOfEntries > 1 Then
                    Me.lblfinalscore.Text = "The Final Score is " & _
                    decFinalScore.ToString("F1")
                Else
                    Me.lblfinalscore.Text = "No score entered"
                End If
        Problem was that strTotalScore2 used in the convert on line 32 was only assigned a value outside of the loop. In this example, you don't need a set of totalscore variables; your total score will always match the final score, so I removed them.

        Also, your code as is will give an error about a positive value if the person enters a score of 0. (See line 43)

        There's also a slight issue on line 47... Maybe I didn't look closely enough, but it doesn't appear that intNumberOfEntr ies will ever be smaller than 1 so the IF block is unnecessary. Maybe you decided to initialise intNumberOfEntr ies after you wrote that part, but it's good idea to remove stuff like that.

        Comment

        • tragic54
          New Member
          • Feb 2008
          • 20

          #5
          i always try and overdo everything when its a simple fix. I've looked everywhere trying to figure this out. Thank you for helping me out, it's greatly appreciated.

          Comment

          Working...