473,396 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. Option Strict On
  2. Option Explicit On
  3.  
  4. Public Class frmScoreboard
  5.  
  6.     Private Sub mnuEnterScoreItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEnterScoreItem.Click
  7.  
  8.         'Declare and initialize variables
  9.         Dim strPointsScored As String
  10.         Dim decPointsScored As Decimal
  11.         Dim strTotalScore2 As String
  12.         Dim decTotalScore2 As Decimal
  13.         Dim strTotalScore As String
  14.         Dim decTotalScore As Decimal
  15.         Dim decFinalScore As Decimal = 0D
  16.         Dim strInputBoxMessage As String = "Enter score #"
  17.         Dim strInputBoxHeading As String = "Points Scored"
  18.         Dim strNormalBoxMessage As String = "Enter score #"
  19.         Dim strNonNumericError As String = "Please enter a numeric value for score #"
  20.         Dim strNegativeNumberError As String = "Please enter a positive value for score #"
  21.  
  22.         'Declare and initialize loop variables
  23.         Dim intNumberOfEntries As Integer = 1
  24.  
  25.         strPointsScored = InputBox(strInputBoxMessage _
  26.         & intNumberOfEntries, strInputBoxHeading, " ")
  27.  
  28.         strTotalScore = strPointsScored
  29.         strTotalScore2 = strTotalScore
  30.  
  31.         Do Until (strPointsScored = "")
  32.             If IsNumeric(strPointsScored) Then
  33.                 decPointsScored = Convert.ToDecimal(strPointsScored)
  34.                 decTotalScore = Convert.ToDecimal(strTotalScore)
  35.                 decTotalScore2 = Convert.ToDecimal(strTotalScore2)
  36.  
  37.                 If decPointsScored > 0 Then
  38.                     Me.lstScores.Items.Add("Points scored " & decPointsScored & _
  39.                     "Total Points " & decTotalScore2)
  40.                     decFinalScore = decPointsScored + decTotalScore2
  41.                     intNumberOfEntries += 1
  42.                     strInputBoxMessage = strNormalBoxMessage
  43.                     decTotalScore2 += Me.lstScores.Items.Add(decTotalScore)
  44.                 Else
  45.                     strInputBoxMessage = strNegativeNumberError
  46.                 End If
  47.  
  48.             Else
  49.                 strInputBoxMessage = strNonNumericError
  50.             End If
  51.  
  52.             If intNumberOfEntries > 0 Then
  53.                 strPointsScored = InputBox(strInputBoxMessage _
  54.                 & intNumberOfEntries, strInputBoxHeading)
  55.  
  56.             End If
  57.  
  58.         Loop
  59.  
  60.  
  61.         Me.lblFinalScore.Visible = True
  62.  
  63.         If intNumberOfEntries > 1 Then
  64.  
  65.             Me.lblFinalScore.Text = "The Final Score is " & _
  66.             decFinalScore.ToString("F1")
  67.         Else
  68.             Me.lblFinalScore.Text = "No score entered"
  69.  
  70.         End If
  71.  
  72.  
  73.    End Sub
  74. End Class
  75.  
Feb 24 '08 #1
4 1573
jagged
23
On Line 35, change decTotalScore2 = Convert.ToDecimal(strTotalScore2) to
decTotalScore2 += Convert.ToDecimal(strTotalScore2)

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

I think Line 43 (decTotalScore2 += Me.lstScores.Items.Add(decTotalScore) ) 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.
Feb 24 '08 #2
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?

Expand|Select|Wrap|Line Numbers
  1. Option Strict On
  2. Option Explicit On
  3.  
  4. Public Class frmScoreboard
  5.  
  6.     Private Sub mnuEnterScoreItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEnterScoreItem.Click
  7.  
  8.         'Declare and initialize variables
  9.         Dim strPointsScored As String
  10.         Dim decPointsScored As Decimal
  11.         Dim strTotalScore2 As String
  12.         Dim decTotalScore2 As Decimal
  13.         Dim decFinalScore As Decimal = 0D
  14.         Dim strInputBoxMessage As String = "Enter score #"
  15.         Dim strInputBoxHeading As String = "Points Scored"
  16.         Dim strNormalBoxMessage As String = "Enter score #"
  17.         Dim strNonNumericError As String = "Please enter a numeric value for score #"
  18.         Dim strNegativeNumberError As String = "Please enter a positive value for score #"
  19.  
  20.         'Declare and initialize loop variables
  21.         Dim intNumberOfEntries As Integer = 1
  22.  
  23.         strPointsScored = InputBox(strInputBoxMessage _
  24.         & intNumberOfEntries, strInputBoxHeading, " ")
  25.  
  26.         strTotalScore2 = strPointsScored
  27.  
  28.         Do Until (strPointsScored = "")
  29.             If IsNumeric(strPointsScored) Then
  30.                 decPointsScored = Convert.ToDecimal(strPointsScored)
  31.                 decTotalScore2 += Convert.ToDecimal(strTotalScore2)
  32.  
  33.                 If decPointsScored > 0 Then
  34.                     Me.lstScores.Items.Add("Points scored " & decPointsScored & _
  35.                     "Total Points " & decTotalScore2)
  36.                     decFinalScore = decPointsScored + decTotalScore2
  37.                     intNumberOfEntries += 1
  38.                     strInputBoxMessage = strNormalBoxMessage
  39.                 Else
  40.                     strInputBoxMessage = strNegativeNumberError
  41.                 End If
  42.  
  43.             Else
  44.                 strInputBoxMessage = strNonNumericError
  45.             End If
  46.  
  47.             If intNumberOfEntries > 0 Then
  48.                 strPointsScored = InputBox(strInputBoxMessage _
  49.                 & intNumberOfEntries, strInputBoxHeading)
  50.  
  51.             End If
  52.  
  53.         Loop
  54.  
  55.  
  56.         Me.lblFinalScore.Visible = True
  57.  
  58.         If intNumberOfEntries > 1 Then
  59.  
  60.             Me.lblFinalScore.Text = "The Final Score is " & _
  61.             decFinalScore.ToString("F1")
  62.         Else
  63.             Me.lblFinalScore.Text = "No score entered"
  64.  
  65.         End If
  66.  
  67.  
  68.     End Sub
  69. End Class
Feb 24 '08 #3
jagged
23
Expand|Select|Wrap|Line Numbers
  1. 'Declare and initialize variables
  2.         Dim strPointsScored As String
  3.         Dim decPointsScored As Decimal
  4.         Dim decFinalScore As Decimal
  5.  
  6.         ' Messages
  7.         Dim strInputBoxMessage As String = "Enter score #"
  8.         Dim strInputBoxHeading As String = "Points Scored"
  9.         Dim strNormalBoxMessage As String = "Enter score #"
  10.         Dim strNonNumericError As String = "Please enter a numeric value for score #"
  11.         Dim strNegativeNumberError As String = "Please enter a positive value for score #"
  12.  
  13.         'Declare and initialize loop variables
  14.         Dim intNumberOfEntries As Integer = 1
  15.  
  16.         strPointsScored = InputBox(strInputBoxMessage & intNumberOfEntries, strInputBoxHeading, " ")
  17.  
  18.         Do Until (strPointsScored = "")
  19.  
  20.             If IsNumeric(strPointsScored) Then
  21.                 decPointsScored = Convert.ToDecimal(strPointsScored)
  22.  
  23.                 If decPointsScored > 0 Then
  24.  
  25.                     decFinalScore += decPointsScored
  26.  
  27.                     Me.lstscores.Items.Add("Points scored " & decPointsScored & _
  28.                     "Total Points " & decFinalScore)
  29.  
  30.                     intNumberOfEntries += 1
  31.  
  32.                     strInputBoxMessage = strNormalBoxMessage
  33.  
  34.                 Else
  35.                     ' What happens if scored = 0 ?
  36.                     strInputBoxMessage = strNegativeNumberError
  37.                 End If
  38.  
  39.             Else
  40.                 strInputBoxMessage = strNonNumericError
  41.             End If
  42.  
  43.             ' Can intNumberOfEntries ever equal 0 ?
  44.             ' Var is init at 1
  45.             'If intNumberOfEntries > 0 Then
  46.             '    strPointsScored = InputBox(strInputBoxMessage _
  47.             '    & intNumberOfEntries, strInputBoxHeading)
  48.             'End If
  49.  
  50.             strPointsScored = InputBox(strInputBoxMessage & intNumberOfEntries, strInputBoxHeading)
  51.  
  52.         Loop
  53.  
  54.  
  55.         Me.lblfinalscore.Visible = True
  56.  
  57.         If intNumberOfEntries > 1 Then
  58.             Me.lblfinalscore.Text = "The Final Score is " & _
  59.             decFinalScore.ToString("F1")
  60.         Else
  61.             Me.lblfinalscore.Text = "No score entered"
  62.         End If
  63.  
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 intNumberOfEntries will ever be smaller than 1 so the IF block is unnecessary. Maybe you decided to initialise intNumberOfEntries after you wrote that part, but it's good idea to remove stuff like that.
Feb 24 '08 #4
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.
Feb 24 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: portroe | last post by:
How can you populate an array using an inputbox(es)? thanks portroe
3
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the...
0
by: Kurt Nesworthy | last post by:
hi, I am struggling trying to put a validation into an exsisting peice of code for an exercise i have got at college. the validation that needs to be inserted is The Pay Grade must be a...
5
by: believeinblue44 | last post by:
I'm new to VB and attempting something (seemingly very very basic by normal standards) and I can not seem to get my Where expression to read values from my inputbox. This is what i have and i am...
1
by: texas22 | last post by:
Here is what I am trying to do I have a spreadsheet that has the column headings of song title, album name, artist name, and genre. I am trying to write a procedure that prompts the user to enter...
0
by: Sammie0 | last post by:
Hello, Over the last two days or so I have been trying to start to learn v8 scripting, Now I am having a few problems with what I am trying to do. Basically I am trying to create a new page...
5
by: Skollie | last post by:
Hi I need help to activate the cancel button in input box to do the comaand "EXIT" in macro here is the macro and no case sensitvityin inputboxes pls tanks Sub Auto_Open() Dim Name As String Dim...
1
by: helraizer1 | last post by:
Hi folks, I am making a spreadsheet for a foreign exchange bureau, on the loading of the page it asks the operator to add the day's exchange rates. I have the following code in Excel VBA ...
3
by: jac130 | last post by:
the program runs, and user is prompted via inputbox to enter an integer-this is the size of the array, then the user fills the array with that many values...but as the user enters the values, i need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.