That was fun, but what would really be lots more fun would be to see the name of the person associated with the score.
That way, if their score is lower than ours, we can poke fun at them. But, we need to stay clear of that clown with the highest score.
Example 5
This time design a Visual Basic.NET program to display the name as well as the score. In a manner
similar to the example above:
This time associate the name of the student with their score.
Enter scores between 0 and 100.
Display the min and max values with the student's name
So we can see who to make fun of, and who to avoid.
The following diagram illustrates 1 possibility of the form at design time:
Figure Array-11: Example 5 Form at Design Time
The following diagram is the same form with the controls desginated by their name property:
Figure Array-12: Example 5 Form at Design Time Expanded
Help Support this site - Click this ad
With the form designed, and all the controls given their name properties, enter the following code in the VB.Form tab:
Public Class Form1
'Declare Array Visible to Entire Class
Private NumberOfScores As Integer = 0
Private MaxEntries As Integer = 20
Private ScoreArray(MaxEntries ) As Double
Private NameArray(MaxEntries ) As String
'Enter Student Name - press <Enter> to place value into Array
Private Sub txtStudentName_Enter (ByVal sender As System .Object , _
ByVal e As KeyEventArgs) Handles txtStudentName .KeyDown
Dim NameEntered As String
If e.KeyCode = keys.Enter then
NameEntered = txtStudentName .Text
If NameEntered = "" then
MessageBox .Show ("You must enter a Name")
txtStudentName .Focus
Exit Sub
End If
NameArray (NumberOfScores)=NameEntered
txtMidtermScore.Clear
txtMidtermScore.Focus
End If
End Sub
'Enter scores - press to place value into Array
Private sub txtQuizScore_Enter (ByVal sender As System .Object , _
ByVal e As KeyPressEventArgs ) Handles txtMidtermScore.KeyPress
Dim EnteredValue As Double
Dim DataString As String
If e.KeyChar = ControlChars.Cr Then
If Double .TryParse (txtMidtermScore .Text ,EnteredValue ) = False then
MessageBox .Show (" You must enter a numeric value like 1 or 2")
txtMidtermScore .Clear
txtMidtermScore .Focus
Exit Sub
End If
If EnteredValue < 0 or EnteredValue >100 then
MessageBox .Show ("Valid range is between 0 and 100, 7 would work " _
& EnteredValue .ToString & " Does not")
txtMidtermScore .Clear
txtMidtermScore .Focus
Exit Sub
End If
If NumberOfScores < MaxEntries then
ScoreArray (NumberOfScores) = EnteredValue
DataString = (NumberOfScores+1) .ToString + ". " + _
NameArray (NumberOfScores).PadRight (15) + _
EnteredValue .ToString.PadLeft (5)
lstDisplay.Items.Add (DataString )
NumberOfScores += 1
End If
txtMidtermScore .Clear
txtStudentName .Clear
txtStudentName .Focus
Exit Sub
End If
' Handles user entering a <TAB> key
If NameArray (NumberOfScores) = "" then
If txtStudentName.Text <> "" then
NameArray (NumberOfScores) = txtStudentName .Text
Else
MessageBox.Show ("you must enter the name first")
txtMidtermScore .Clear
txtStudentName .Clear
txtStudentName .Focus
End If
End If
End Sub
'Clear Last Entry
Private Sub btnClearLast_Click( ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClearLast.Click
NumberOfScores -= 1
lstDisplay .Items .RemoveAt (NumberOfScores )
txtMidtermScore .Clear
txtStudentName .Clear
txtStudentName .Focus
End Sub
'Clear All Entries
Private Sub btnClearAll_Click( ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnClearAll.Click
lstDisplay .Items .Clear
NumberOfScores = 0
txtMidtermScore .Clear
txtStudentName .Clear
txtStudentName .Focus
End Sub
'Calculate Min, Max and Average
Private Sub btnStatistics_Click( ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnStatistics.Click
Dim Output As String
Dim Average As Double = 0
Dim Sum As Double = 0
Dim ScoreNumber As Integer
Dim MinScore As Double = ScoreArray (0)
Dim MaxScore As Double = ScoreArray (0)
lstDisplay .Items .Add ("")
For ScoreNumber =0 to NumberOfScores - 1
Sum+=ScoreArray (ScoreNumber)
If(MinScore > ScoreArray (ScoreNumber) ) then
MinScore = ScoreArray (ScoreNumber)
End If
If(MaxScore < ScoreArray (ScoreNumber)) then
MaxScore = ScoreArray (ScoreNumber)
End If
Next
If(NumberOfScores>0 ) then
Average = Sum/NumberOfScores
End If
Output ="Average: " + Average .ToString(".0")
lstDisplay .Items .Add (Output )
Output ="Minimum: " + MinScore .ToString
lstDisplay .Items .Add (Output )
Output ="Maximum: " + MaxScore .ToString
lstDisplay .Items .Add (Output )
txtMidtermScore .Clear
txtStudentName .Clear
txtStudentName .Focus
End Sub
'Set the focus to the Student Name at Run Time
Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim MessageString As String
Me.ActiveControl=txtStudentName
MessageString = "First, Enter the student Name and Press , " +vbCrLf
MessageString += "then enter the student score and Press " +vbCrLf
MessageString += "The Name and Score will be displayed in the Score Display"
MessageBox.Show (MessageString)
End Sub
End Class
Figure Array-13: Example 5 Source Code.
Help Support this site - Click this ad
There are a couple of new concepts introduced in this code section:
Private NumberOfScores As Integer = 0: Initialize the array index to 0
Private MaxEntries As Integer = 20: This will be the maximum size of the array.
Larger arrays will be declared dynamically, since this is just an example - fixed size is okay.
Private ScoreArray(MaxEntries ) As Double: This is the array containing the numeric data of students course scores.
Private NameArray(MaxEntries ) As String: This is the parallel array containing the names of the students in the course.
Private Sub txtStudentName_Enter (ByVal sender As System .Object , ByVal e As KeyEventArgs) Handles txtStudentName .KeyDown
Private Sub txtStudentName_Enter: This is the routine that is called every time a key is pressed in the student name
TextBox, txtStudentName, each time checking for the <Enter> key to be pressed.
ByVal sender as System.Object: Sender is Visual Basic's reference to the TextBox control.
ByVal e as KeyEventArgs: e is the key information occuring every time a key is detected in the down position.
Handles txtStudentName.KeyDown: This is the routine to be called when Windows detects a Key in the txtStudentName control.
Private sub txtMidtermScore_Enter (ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Handles txtMidtermScore.KeyPress
Private sub txtMidTermScore_Enter: This function will be called when the <Enter> key is pressed
in the TextBox.
ByVal sender As System.Object: A reference to the TextBox control.
ByVal e As KeyPressEventArgs e is every key pressed. KeyPressEventArgs decodes which key is pressed.
Handles txtMidTermScore.KeyPress: Call this subroutine after every key press in the MidtermScoreTextBox.
Dim EnteredValue As Double: This will be the numeric variable which receives the number entered in the textbox.
Dim DataString as String: A String used to build a reflection of the data entered to be displayed in the ListBox.
If e.KeyChar = ControlChars.Cr Then: Test for <Enter> key.
e.KeyChar: The last key pressed in the TextBox.
e: is the key pressed on the keyboard.
KeyChar: is used as the reference in the KeyPressEventArgs class descriptor.
ControlChars.Cr The comparison value for the <Enter> key when using KeyPressEventArgs.
ControlChars: is used to decode KeyChar.
Cr: is the value associated with the <Enter> key.
If Double .TryParse (txtQuizScore .Text ,EnteredValue ) = False then: Check for Numeric Value.
Double.TryParseSearch the text for a value of type double.
txtMidtermScore.Text: The text is the Quiz Score TextBox is what will be scanned for a numeric value.
EnteredValue: The recipient of the Double value if the scan is successful.
=False then: Text was in improper format - execute code on next line.
MessageBox .Show (" You must enter a numeric value like 1 or 2"): Display a message box gently guiding the
user to the correct response.
txtMidterm .Clear: Remove the information that was previously in the TextBox.
txtMidtermScore.Focus: Place the Cursor in the empty TextBox, so they start right in entering data.
Exit Sub: Error in Data Entry, no point in going any further.
End If: End of Data Entry check for non-numeric value.
If EnteredValue < 0 or EnteredValue >10 then: Check for valid range between 0 and 10.
If NumberOfScores < MaxEntries then: Make sure they aren't getting too ambitious, and going out of bounds on the array.
ScoreArray (NumberOfScores) = EnteredValue: All is good, place value in next array location.