The form at Design Time should look similar to the diagram below:
Figure Array-7: Form at Design Time
The form at design time with the controls expanded is shown below:
Figure Array-8: Form at Design Time (expanded)
Help Support this site - Click this ad
Enter the code as shown below:
Public Class Form1
'Declare Array Visible to Entire Class
Private NumberOfScores As Integer = 0
Private MaxEntries As Integer = 20
Private ScoreArray(MaxEntries ) As Double
'Enter scores - press <Enter> to place value into Array
Private sub txtQuizScore_Enter (ByVal sender As System .Object , _
ByVal e As KeyPressEventArgs ) Handles txtQuizScore.KeyPress
Dim EnteredValue As Double
Dim DataString As String
If e.KeyChar = ControlChars.Cr Then
If Double .TryParse (txtQuizScore .Text ,EnteredValue ) = False then
MessageBox .Show (" You must enter a numeric value like 1 or 2")
txtQuizScore .Clear
txtQuizScore .Focus
Exit Sub
End If
If EnteredValue < 0 or EnteredValue >10 then
MessageBox .Show ("Valid range is between 0 and 10, 7 would work " _
& EnteredValue .ToString & " Does not")
txtQuizScore .Clear
txtQuizScore .Focus
Exit Sub
End If
If NumberOfScores < MaxEntries then
ScoreArray (NumberOfScores) = EnteredValue
NumberOfScores += 1
DataString = NumberOfScores .ToString + ". " + EnteredValue .ToString
lstDisplay.Items.Add (DataString )
End If
txtQuizScore.Clear
txtQuizScore .Focus
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 )
txtQuizScore .Clear
txtQuizScore .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
txtQuizScore .Clear
txtQuizScore.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
lstDisplay .Items .Add (Output )
Output ="Minimum: " + MinScore .ToString
lstDisplay .Items .Add (Output )
Output ="Maximum: " + MaxScore .ToString
lstDisplay .Items .Add (Output )
End Sub
End Class
Figure Array-9: Visual Basic Code to Meet specifications
Help Support this site - Click this ad
The form at design time with the controls expanded is shown below:
Let's take a look at the code in action:
Private NumberOfScores As Integer = 0: This tells us the size of the array. It is accessible in the entire class.
Private MaxEntries As Integer = 20: Maximum number of entries allowed in this example.
Private ScoreArray(MaxEntries ) As Double: The Array of Quiz scores is also accessible to all functions on this form.
Private sub txtQuizScore_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 txtQuizScore.KeyPress: Call this subroutine after every key press in the TextBox.
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.
txtQuizScore.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 with some smart aleck comment
for those that refuse to follow directions.
txtQuizScore .Clear: Remove the information that was previously in the TextBox.
txtQuizScore.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.
Help Support this site - Click this ad
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.
NumberOfScores += 1: Move to the next Array location.
Output ="Maximum: " + MaxScore .ToString: Create and Display Maximum value.
Help Support this site - Click this ad
Since we should have a rough idea what's going on in the program, let's take this puppy out for a spin, and see what
we get. We'll use the following data, and see if the program behaves as expected:
Keys Entered
Displayed Output
5Enter
1. 5
6Enter
1. 5
2. 6
7Enter
1. 5
2. 6
3. 7
8Enter
1. 5
2. 6
3. 7
4. 8
Clear Last
1. 5
2. 6
3. 7
Statistics
1. 5
2. 6
3. 7
Average: 6
Minimum: 5
Maximum: 7
Figure Array-10: Form At Run Time
Yup, kind of what we thought. If you're an ambitious type, you can press the Clear All and re-enter the data
to make sure that the array resets. When you're done, you can catch up to the rest of us with the next exciting project: Parallel Arrays.
In order to continue this guide and work on that fascinating topic
Parallel Arrays press the
Button Below: