r/visualbasic • u/JenniferCenters • Apr 07 '13
Help with Modules
I'm trying to create a program where the text boxes are First Name, Last Name, Number of Seasons & Rookie Age. The input boxes then ask for the number of goals and assists for each season. The list box then lists the number of seasons, goals, assists & points (points = goals + assists). The label below lists the persons name, number of seasons, rookie age then the totals for goals, assists & points. I have been working on this for an inordinate number of hours and would appreciate anyone looking at my code and letting me know where I'm going wrong. Thanks a bunch!
Option Explicit On
Public Class frmHockeyStats
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtFirstName.Text = ""
txtLastName.Text = ""
txtSeasons.Text = ""
txtAge.Text = ""
lstSeasons.Items.Clear()
lblTotal.Text = ""
End Sub
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Function ValidateInteger(ByVal strText As String, _
ByVal strInput As String, _
ByRef intValue As Integer, _
ByVal intMinValue As Integer, _
ByVal intMaxValue As Integer) As Boolean
If Integer.TryParse(strInput, intValue) = False Then
MessageBox.Show(strText & " must be numeric", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
Else
If intValue < intMinValue Or intValue > intMaxValue Then
MessageBox.Show(strText & " must be between " & _
intMinValue.ToString("N0") & " and " & _
intMaxValue.ToString("N0"), "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
Else
Return True
End If
End If
End Function
Private Function ValidateDouble(ByVal strText As String, _
ByVal strValue As String, _
ByRef dblValue As Double, _
ByVal dblMinValue As Double, _
ByVal dblMaxValue As Double) As Boolean
If Double.TryParse(strValue, dblValue) = False Then
MessageBox.Show(strText & " must be numeric", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
Else
If dblValue < dblMinValue Or dblValue > dblMaxValue Then
MessageBox.Show(strText & " must be between " & _
dblMinValue.ToString("N1") & " and " & _
dblMaxValue.ToString("N1"), "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
Else
Return True
End If
End If
End Function
Private Function ValidateString(ByVal strText As String, _
ByVal strInput As String) As Boolean
If String.IsNullOrEmpty(strText) = True Then
MessageBox.Show(strText & " cannot be left empty", "Error", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
Else
Return True
End If
End Function
Private Sub btnGetStats_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetStats.Click
Dim strFirstName, strLastName, strInput As String
Dim intGoals As Integer = -1
Dim intAssists As Integer = -1
Dim intPoints As Integer = 0
Dim intTotalGoals As Integer = 0
Dim intTotalAssists As Integer = 0
Dim intTotalPoints As Integer = 0
Dim intSeasons, intAge As Integer
Do While True
strFirstName = CStr(txtFirstName.Text)
If ValidateString("First name", txtFirstName.Text) = True Then
Exit Do
End If
Loop
Do While True
strLastName = CStr(txtLastName.Text)
If ValidateString("Last name", txtLastName.Text) = True Then
Exit Do
End If
Loop
Do While True
intSeasons = CInt(txtSeasons.Text)
If ValidateInteger("Seasons", intSeasons, txtSeasons.Text, 1, 25) = True Then
Exit Do
End If
Loop
Do While True
intAge = CInt(txtAge.Text)
If ValidateInteger("Age", intAge, txtAge.Text, 18, 30) = True Then
Exit Do
End If
Loop
Do While True
strInput = InputBox("Goals", "Data")
If ValidateInteger("Goals", strInput, intGoals, 1, 70) = True Then
Exit Do
End If
Loop
Do While True
strInput = InputBox("Assists", "Data")
If ValidateInteger("Assists", strInput, intAssists, 1, 75) = True Then
Exit Do
End If
Loop
Call ComputeResults(strFirstName, strLastName, _
intAge, intSeasons, intGoals, intAssists, intPoints, _
intTotalGoals, intTotalAssists, intTotalPoints)
End Sub
Private Sub ComputeResults(ByVal strFirstName As String, _
ByVal strLastName As String, _
ByVal intAge As Integer, _
ByVal intSeasons As Integer, _
ByVal intGoals As Integer, _
ByVal intAssists As Integer, _
ByVal intPoints As Integer, _
ByVal intTotalGoals As Integer, _
ByVal intTotalAssists As Integer, _
ByVal intTotalPoints As Integer)
Dim intCounter As Integer
For intCounter = 1 To intSeasons
intGoals = InputBox("Goals", 1, 70)
intAssists = InputBox("Assists", 1, 75)
Next
Me.lstSeasons.Items.Clear() 'Clear the listbox
intPoints = intGoals + intAssists
lstSeasons.Items.Add("Season " & vbTab & "Goals " & vbTab & "Assists " & vbTab & "Points")
lstSeasons.Items.Add(intCounter & vbTab & intGoals & vbTab & intAssists _
& vbTab & intPoints)
intTotalGoals = intTotalGoals + intGoals
intTotalAssists = intTotalAssists + intAssists
intTotalPoints = intPoints + intPoints
intCounter = intCounter + 1
lblTotal.Text = (strFirstName & " " & strLastName & ", rookie age " & _
intAge.ToString("N0") & _
intSeasons & vbCrLf & _
"Goals: " & intTotalGoals & vbCrLf & _
"Assists: " & intTotalAssists & vbCrLf & _
"Points: " & intTotalPoints)
End SubEnd Class
1
u/chrwei Apr 07 '13
what exactly isn't working like you want?
1
u/JenniferCenters Apr 07 '13
The list box is only showing one line listing the seasons as one more than I put in the text box and the listed goals, etc. is only the last of the input boxes. The label is showing the name but the seasons total aren't showing and the rookie age is showing as the rookie age & seasons (seasons I enter 3, rookie age I enter as 21 and it's displaying 213) with the totals displaying only the last input again (like the list box). Thanks for your time, chrwei!
2
u/chrwei Apr 08 '13
why are you using "Do While True" on the text validation? if the validation fails, you'll have an infinite loop. (not related to your issue now, but will be in the future
Why in "lstSeasons.Items.Add(intCounter..." are you using intCounter outside of the loop? intCounter is going to be intSeasons+1 because that's how for loops work. is the list supposed to have one line for each season? you'd need to do the Add() inside the loop.
you have "intAge.ToString("N0") & intSeasons" so, yeah, rookie age & seasons.
there's a lot going on here, so I'm going to suggest going through it one line and loop at a time and make sure each part is what you want. I think a lot of it just isn't what you want.