locked
Add items to listbox

    Question

  • Hello, I'm trying to make a project where when the user clicks btnCalculate an input box shows asking to input SPEED in miles per hour, followed by another inputbox asking for the amount of TIME in hours. It should then use a loop to display in a listbox on the form the distance the vehicle traveled for each hour of that time period. This is what I have thus far but I am stuck once again!

    This is an example of the output in the listbox:

    Vehicle Speed : 60 MPH
    Time Traveled : 7 Hours
    Hour        Distance traveled
    1             60
    2             120
    3             180
    4             240
    5             300
    6             360
    7             420

     

    Dim speed As Short

    Dim hours As Short

    Dim distance As Short

    Dim count As Short

    speed = InputBox("Enter your speed", "Speed", "60")

    hours = InputBox("Enter time traveled", "Time", "7")

    distance = speed * hours

    Do While count < distance

    count += 1

    Loop

    lstOutput.Items.Add(distance)

     

    Monday, November 06, 2006 7:31 PM

All replies

  • Hi,

    take a look at the following code:

    Dim speed As Short
    Dim hours As Short

    speed = InputBox("Enter your speed", "Speed", "60")
    hours = InputBox(
    "Enter time traveled", "Time", "7")
    ' Check for speed and hours correct input here, we covered this today already :)

    For hourTravelled As Integer = 1 To hours
       
    lstOutput.Items.Add("Hour: " & hourTravelled & ", distance: " & hourTravelled * speed)
    Next

    Andrej

    Monday, November 06, 2006 7:44 PM
  • Hi,

    I was quicker to write it for you than to describe how to change your code, something tells me your doing your homework so maybe comment whats going on.

     Dim speed As Short = 0
     Dim total_hours As Short = 0
     Dim current_distance As Short = 0
     Dim current_hour As Short = 1

     speed = Short.Parse(InputBox("Enter your speed", "Speed", "60"))
     total_hours = Short.Parse(InputBox("Enter time traveled", "Time", "7"))

     Do While current_hour <= total_hours
      current_distance = speed * current_hour
      current_hour = current_hour + 1
      lstOutput.Items.Add(current_distance)
     Loop

    Monday, November 06, 2006 7:52 PM
  • Thank you very much. Very helpful. Here's how I filled in the rest:

     

    Dim speed As String

    Dim hours As String

    Dim distance As Short

     

    speed = InputBox("Enter your speed", "Speed", "60")

    If IsNumeric(speed) = False Or Val(speed) <= 0 Then

    MessageBox.Show("Please Enter A Positive Speed in Number Form like 2, 17, or 386")

    End If

    hours = InputBox("Enter time traveled", "Time", "7")

    If IsNumeric(hours) = False Or Val(hours) <= 0 Then

    MessageBox.Show("Please Enter A Positive Number of Hours in Number Form like 1, 26,or 524")

    Exit Sub

    End If

    distance = speed * hours

    lstOutput.Items.Add("Vehicle speed: " & (speed) & " MPH")

    lstOutput.Items.Add("Time Traveled: " & (hours) & " Hours")

    lstOutput.Items.Add("Hour " & "Distance Traveled")

    lstOutput.Items.Add("---------------------------------------")

    For hoursTraveled As Integer = 1 To hours

    lstOutput.Items.Add(hoursTraveled & " " & hoursTraveled * speed)

    Next

    lstOutput.Items.Add("-----------------------------------------")

    lstOutput.Items.Add("Total Distance: " & (distance) & (" miles."))

     

    Monday, November 06, 2006 8:20 PM
  • That works too, thank you!

    It is not homework but little practice apps in my Intro to Programming textbook. I don't get any help from anyone in class because I think no one else studys or does these as they are not required. So I just do them as practice, but thanks for the help! Doing these do however help me with the actual homework challenges at the end of the chapter.

    Monday, November 06, 2006 8:25 PM