test score form

kaddu747

New member
Joined
Sep 30, 2005
Messages
4
Programming Experience
1-3
So i' new to VB.NET and im making a windows form where the user inputs testscores and the form updates total number of scores added,average score,best score,number of scores.

I also need to validate the user's input not only as a number but also as an integer.I figure i need a while loop to do this and some sub's and functions too. i have the code-so-far-below with the 'If' loop..pardon that...i need to put a while loop..but any more help on validating an integer would help.


Private Sub btnEnter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnter.Click

Dim bValidEntries As Boolean
Me.ValidateScoreEntry(txttestscore.Text, bValidEntries)
If bValidEntries Then
End If
End Sub

Private Sub ValidateScoreEntry(ByVal txttestscore As String, ByRef ValidEntries As Boolean)

Dim daveragescore, dbestscore As Decimal
Dim itestscore, inumberofscores, itotalscores As Integer

If IsNumeric(txttestscore) Then
If txttestscore >= 0 And txttestscore <= 100 Then
inumberofscores += 1
itotalscores += txttestscore
daveragescore = itotalscores / inumberofscores
txtnumberofscores.Text = inumberofscores
txttotalscores.Text = itotalscores
txtaveragescore.Text = daveragescore
ValidEntries =
True

Else : MsgBox("Test Score not between 0 and 100", MsgBoxStyle.Critical, "Error")
End If

Else : MsgBox("Test Score is not a number!", MsgBoxStyle.Critical, "Error")
End If

End Sub
 
when posting code, it's easier to read if you put it between the [ code] and [ /code] tags

anywho when i need a textbox to only allow numeric characters (numbers) then i use the textbox's KeyPress event to check each character and only allow numbers like this:
VB.NET:
Private Sub Textbox1_KeyPress(...) Handles Textbox1.KeyPress
  If IsNumeric(e.KeyChar) = False then e.Handled = True
End Sub
then you dont have to check it anywhere else, but there is also a NumericUpDown control that only allows numbers which in this case might better suit the need

as for the Loop: i dont think you would really need a loop in this case, there's only a certain number of scores being entered here so i would instead in the btnEnter's Click event i would add up all the scores then give the totals:
VB.NET:
Private Sub btnEnter_Click(...) Handles btnEnter.Click
  Dim decScoreTotals as Decimal
  Dim intTotalScores as Integer

  If txtScore1.Text <> "0" Then
    intTotalScores += 1
    decTotalScores += CDec(txtScore1.Text)
  End If

  If txtScore2.Text <> "0" Then
     intTotalScores += 1
     decTotalScores += CDec(txtScore2.Text)
   End If

  If txtScore3.Text <> "0" Then
     intTotalScores += 1
     decTotalScores += CDec(txtScore3.Text)
   End If

  lblAverageScore.Text = CStr(DecTotalScores / CDec(intTotalScores))
End Sub
i hope this helps
 
Last edited:
I'm just learning too and just went through the same problem. I'm using JuggaloBrotha's code to make sure the entry is definitely a positive integer.

But in order to check that the number is between 0 and 100, you can either make sure it stays that way while the person is typing, or you can be more forgiving and wait until the user leaves the box to check. The code is exactly the same except for the event that is handled (assuming that your text box is called txttestscore):

VB.NET:
To test the entry as the person leaves the textbox:
 
    Private Sub txttestscore_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txttestscore.Leave
        if txttestscore < 0 or txttestscore > 100 then
            MsgBox("Test Score not between 0 and 100", MsgBoxStyle.Critical, "Error")
        end if
    End Sub
 
Or to check it while the person is typing and not when they leave the box:
 
    Private Sub txttestscore_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txttestscore.TextChanged
        if txttestscore < 0 or txttestscore > 100 then
            MsgBox("Test Score not between 0 and 100", MsgBoxStyle.Critical, "Error")
        end if
    End Sub
 
to expand on MyForumID's post, instead of using a messagebox to inform the user you could simply make the textbox = 100 if it's too large, or = 0 if it's a negative number:
VB.NET:
'To test the entry as the person leaves the textbox:
Private Sub txtTestScore_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTestScore.Leave
  If Val(txtTestScore.Text) > 100 Then txtTestScore.Text = "100"
  If Val(txtTestScore.Text) < 0 Or txtTestScore.Text = "" Then txtTestScore.Text = "0"
End Sub
I would personally use the Leave or the LostFocus event to check if the value is too great or too small
 
Back
Top