1 Replies - 459 Views - Last Post: 03 November 2011 - 03:48 PM Rate Topic: -----

#1 DaveHumphrey7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-November 11

Can not get user input data into the loop of the input box

Posted 03 November 2011 - 12:43 PM


Option Strict On

Public Class Form1

    Private Sub btnDistance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDistance.Click
        'The btnEnterSpeed click event accepts and displays up to ten speeds from the user
        'and then calculates and displays average speed

        'Declare and initialize variable

        Dim intSpeedLimit As Integer
        Dim strHours As String
        Dim decHours As Decimal = 0D
        Dim decTotalHours As Decimal = 0D
        Dim decTotalDistance As Decimal
        Dim strInputMessage As String = "Enter the Hours for Day #"
        Dim strInputHeading As String = "Hours Per Day"
        Dim strNormalMessage As String = "Enter the Hours for Day #"
        Dim strNonNumericError As String = "Error - Enter a number for Day #"
        Dim strNegativeError As String = "Error - Enter positive number for Day #"

        'Declare and initalize loop variable

        Dim strCancelClicked As String = ""
        intSpeedLimit = Convert.ToInt32(txtSpeedLimit.Text)
        strHours = txtNumberofDays.Text
        Dim intEndCount As Integer
        Dim intEndHours As Integer
        Dim intNumberofHours As Integer = 1



        'This loop allows the user to enter the speed of up to 10 vehicles.
        'The loop terminates when the user has entered 10 speeds or the user
        'click the cCancel button or the Close button in the InputBox

        strHours = InputBox(strInputMessage & intNumberofHours, strInputHeading, " ")

        Do Until intEndCount > intEndHours Or strHours = strCancelClicked

            If IsNumeric(strHours) Then
                decHours = Convert.ToDecimal(strHours)
                If decHours > 0 Then
                    lstHours.Items.Add(decHours)
                    decTotalHours += decHours
                    intNumberofHours += 1
                    strInputMessage = strNormalMessage
                Else
                    strInputMessage = strNegativeError
                End If
            Else
                strInputMessage = strNonNumericError
            End If

            If intNumberofHours <= intEndHours Then
                strHours = InputBox(strInputMessage & intNumberofHours, strInputHeading, " ")
            End If

        Loop

        ' Makes label visible
        lblTotalNumberofMiles.Visible = True

        'Calculates and displays average speed
        If intNumberofHours > 1 Then
            decTotalDistance = intSpeedLimit * decTotalHours
            lblTotalNumberofMiles.Text = "Total Miles you will drive " & decTotalDistance.ToString("F1") & " Miles"
        Else
            lblTotalNumberofMiles.Text = "Hours entered"
        End If

        'Disable the Enter Speed button
        btnDistance.Enabled = False

    End Sub

    Private Sub mnuClear_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClear.Click
        'The mnuClear click event clears the listbox and hides the
        'average speed label. It also enables the Enter Speed button

        lstHours.Items.Clear()
        lblNumberofDays.Visible = False
        btnDistance.Enabled = True

    End Sub

    Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        ' The mnuExit click event closes the window and exits the application

        Close()

    End Sub

End Class



I cannot figure out how to incorporate the number of days into the input box then get it to loop and count the number of times the User inputs in the number of days. Then I have to incorporate it into the equation to get the total distance D = MPH * Hours. What I have right now wil not count up the number of days and locks up after I input one number and will not add anything to the listbox. Because I also have to make sure that they cant add nonnumaric values and the number of hours per day cannot exceed 20. I have put things in and taken them out but it has been 2 days and I am lost now

Is This A Good Question/Topic? 0
  • +

Replies To: Can not get user input data into the loop of the input box

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4334
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Can not get user input data into the loop of the input box

Posted 03 November 2011 - 03:48 PM

Let's take a look at a specific section of code here...


Dim intEndCount As Integer ' Initialized to zero
Dim intEndHours As Integer ' Initialized to zero
Dim intNumberofHours As Integer = 1

strHours = InputBox(strInputMessage & intNumberofHours, strInputHeading, " ")

Do Until intEndCount > intEndHours Or strHours = strCancelClicked



In the code above, intEndCount and intEndHours are created as integers, but what value do they hold? They are initialized to zero.

So we have a variable called strCancelClicked which has been declared as an empty string.

Now let's assume you enter a value. This will mean strHours will not equal "" and so will enter the loop. It progresses through and increments intNumberOfHours by 1. It will then go out of the if statements and hit this next if statement....

If intNumberofHours <= intEndHours Then
     strHours = InputBox(strInputMessage & intNumberofHours, strInputHeading, " ")
End If



Now intNumberofHours will be 2 and intEndHours will be zero. Will 2 ever be less than or equal to 0? no, so you will never reach inside this if statement. It will go back to the top and strHours will still have the value originally typed in by the user and the whole loop will continue, never having reached a condition where that if statement will be true and strHours will be reset with another InputBox request.

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1