armyfrenchy Level: Trainee
 Registered: 13-04-2010 Posts: 2
|
help with my vb code PLEASE??
Can someone please fill in the banks? I am totally confused!! This program needs to list in the listbox the distance a car travelled in miles based on speed and travelled time in hours...with a popup box that appears if the values entered are not numerical? Formula is distance=speed*time. When the user clicks on the button calculate, two Inputboxes need to appear asking for speed and distance. It is supposed to work with listboxes, arrays, and inputboxes, and have validations if user enters string instead of numbers...
I am not sure what to put in the listbox lblist subroutine to make what the user inputted appear? here is the assignment
Loops
 Data validation
 Decision structures
 Sub Procedures
 InputBox Function
 MessageBox Function
 Modularity concept
Program:
This program shall determine the distance that a vehicle travels (in miles) based on the vehicle’s speed (in MPH), and the traveled time (in hours).
NOTE: In most applications, the InputBox function should not be used as the primary method of input because it draws the user’s attention away from the application’s form. It also complicates data validation because if a data entry error occurs, the input box closes not allowing the user for another opportunity for coming back and fixing the error. Despite these drawbacks, it is a convenient tool for developing and testing applications.
The formula for determining the distance traveled is given by:
Distance = Speed * Time
Create an application having a graphical user interface (GUI) similar to the Main Form shown in Figure 1. When the user clicks the Calculate button, the application displays an InputBox asking the user to enter the speed of the vehicle in miles-per-hour (MPH), followed by a second InputBox asking the user to enter the traveled time (in hours) If the user enters non-numeric data or leaves empty the contents in either of the InputBoxes, then the user receives the appropriate error message in a MessageBox. To finalize the project requirements, the program uses a Loop structure to display in a ListBox the distance that the vehicle traveled during each hour, and the total distance traveled (Figure 5).
In order to meet the Modularity requirement of this project, the SpeedErrorMessage () and TimeErrorMessage () sub procedures must be called from inside the CalculateDistance() routine (sub procedure) and executed only when the user enters non-numeric data into the
corresponding Input Boxes (speed and time). The validation requirements include:
 The vehicle’s speed of travel must be numeric (in MPH) and greater than zero,
otherwise, display to the user in a MessageBox the appropriate error message.
 The vehicle’s time of travel must be numeric (in hours) and greater than zero,
otherwise, display to the user in a MessageBox the appropriate error message.
 The Total travel distance must be numeric.
This is what i got so far:
Public Class Form1
Private Sub lbList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstOutput.SelectedIndexChanged
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim intSpeed As Integer
Dim intTime As Integer
Dim intCount As Integer
Dim intDistance As Integer
Dim strDriver As String
Dim strTapeMessage As String
strDriver = InputBox("What's yer name?")
If strDriver = "" Then
DriverErrorMessage(strDriver)
Exit Sub
End If
Try
intSpeed = CInt(InputBox("Enter the vehicle's speed, in MPH.", "Input Needed"))
Catch
SpeedErrorMessage() ' calling the subproceduree as part of the modulatity requirement.
Return
End Try
If intSpeed <= 60 Then
Lawfullspeed(strDriver) ' calling the method to display a message to the user
Else
UnLawfullspeed(strDriver) ' calling the method to display a message to the user
End If
' Validate input.
If intSpeed < 1 Then
' Display error message.
SpeedErrorMessage() ' calling the subproceduree as part of the modulatity requirement.
Else
Try
' Get the number of intHours.
intTime = CInt(InputBox("How many hours have you traveled?", "Input Needed"))
Catch
If intTime < 1 Then
TimeErrorMessage() ' calling the subproceduree as part of the modulatity requirement.
End If
End Try
End If
If intSpeed <= 60 Then
Lawfullspeed(strDriver) ' calling the method to display a message to the user
Else
UnLawfullspeed(strDriver) ' calling the method to display a message to the user
End If
lstOutput.Items.Add(" Vehicle speed: " & intSpeed & " MPH")
lstOutput.Items.Add(" Time traveled: " & intTime & " hours")
lstOutput.Items.Add(" Hours Distance Traveled")
lstOutput.Items.Add(" ----------------------------------------")
For intCount = 1 To intTime
intDistance = intCount * intSpeed
lstOutput.Items.Add(" " & intCount & vbTab & intDistance)
Next intCount
lstOutput.Items.Add(" ----------------------------------------")
' displays the driver's name in upper case, _ the speed of travel, and a Lawful message
If intSpeed <= 60 Then
lstOutput.Items.Add("Dear " & " " & UCase(strDriver) & " " & _
"you traveled at a Lawful speed of " & " " & intSpeed & " " & "MPH")
Else
' displays the driver's name in upper case, the speed of travel, and a Unlawful message
strTapeMessage = " and you got caught on tape!"
lstOutput.Items.Add("Dear " & " " & UCase(strDriver) & " " & _
"you traveled at an UNLAWFUL speed of " & " " & intSpeed _
& " " & "MPH")
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub SpeedErrorMessage()
MessageBox.Show("The speed must must be an integer value", "Input Error")
Return
End Sub
Private Sub TimeErrorMessage()
MessageBox.Show("The travel time must be an integer value in hours", "Input Error")
Return
End Sub
Private Sub DriverErrorMessage(ByVal a As String)
MessageBox.Show("What, your mom didn't name you?", "Input Error")
Return
End Sub
Private Sub Lawfullspeed(ByVal a As String)
MessageBox.Show("Congratulations for being a conformist", "Mr. " & UCase(a) & " " & ".")
Return
End Sub
Private Sub UnLawfullspeed(ByVal a As String)
MessageBox.Show("What's your hurry", "Mr. Big Shot " & UCase(a) & " " & "?")
Return
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
[Edited by armyfrenchy on 13-04-2010 at 07:57 PM GMT]
|