Modify the label parameters as shown in the diagram below
Border Style ==> Fixed3D
Font ==> Courier New
(Name) ==> lblShow
Figure Array-4: Label control properties for Example 3
Once the Design Form has been created. Double-Click somewhere on the form and enter the following code:
Public Class Form1
Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Const ArraySize As Integer = 25
Dim Score(ArraySize ) As Integer
Dim Index As Integer
lblShow.Text = ""
For Index = 0 to ArraySize
Score (Index) = cint(Rnd () * 100)
lblShow .Text &= Index .ToString + ". " + Score (Index).ToString +vbCrLf
Next
End Sub
End Class
Figure Array-5: Source Code for Example 3 (Form Load)
Help Support this site - Click this ad
Let's take a quick look at the code:
Const cSize As Integer = 25:
Const: Const means constant, and the programmer is not allowed to change this value at a later time.
ArraySize: This will be the name of the value we use to assign a size to the Array.
As Integer: Array indexes in Visual Basic must be of type Integer.
=25: This is the constant size of the array, and the only time ArraySize will accept a value.
Dim Score(ArraySize) As Integer:
Dim: This means that Score will only be valid where it is declared: Sub Form1
Score(ArraySize): An Array named Score that has 25 elements in it.
As Integer: The elements in Score, will only be integers.
Dim Index as Integer: Index will be used to index through our Score array, and only integers can be used
as an index in Visual Basic.
lblShow.Text = "": Clear all previous text from the label output control.
For Index = 0 to ArraySize: Index is going to select every element in the array Score sequentially.
lblShow.Text This is the text portion of the visible label on the form.
&=: This is shorthand meaning add what is on the right hand side to what is already on the left hand side.
Index.ToString: This will place the index value in front of the score value during the listing (i.e. 1. 2. 3....)
". ": This puts a decimal point after the array index to isolate it from the Test Score value.
Score(Index).ToString: This turns the number in the array to a string so we can put it on the screen.
vbCrLf: Visual Basic New Line symbol - causes the text to go down to the next line.
Help Support this site - Click this ad
Once you have entered the code correctly, press F5 to run the program. You should get a form like the one below:
Figure Array-6: Form At Run Time
I bet you're glad you didn't get the same score on your test as my Student #6. He should have stayed home, and
opened a six-pack of Bud Lite and watched some TV. I always say: "Why work for an F when you can get one for free?" Student #15 didn't
do so well either, but that pack of Skittles they gave the instructor right before the test, seems to have nudged that score up a couple of points.
The ListBox is one of the intrinsic controls in Visual Basic.NET. It can be found in the Visual Basic Toolbox located
on the left side of the screen. The ListBox has several applications. One of the applications is presenting specific choices to the user, and allowing the
user to select one of them. Using the ListBox as output, can allow you to give the user different selections, depending on what they have previously entered
for their preferences. If you place more entries in the ListBox then you have given it room for on the screen, will cause the ListBox to give you a vertical
scroll bar on the right side. When a user clicks on a ListBox entry, you can program specific actions for each of the ListBox entries. (This is where setting
the ListBox options in an array, simplifies the task.) The ListBox can be resized on the form by the user at RunTime. We will introduce using the
ListBox control in the next example:
Example 4
Design a Visual Basic.NET program to help your instructor display Quiz scores, and compute their
average. The quiz scores will be entered manually. Also assume that no more than 20 students will be present to take the Quiz at any time, since
it is just the first week after spring break, and most of the class is still recovering.
But first, let us review the Program Development Life Cycle:
Analyze the Problem:
Determine the input requirements.
The user will manually enter the quiz scores.
The user will request an analysis be performed upon completion of Quiz score entry.
Determine Output Specifications.
Display Individual Scores.
Display Average of All scores.
Display Number of Scores entered.
Display Maximum and Minimum Scores entered.
Processing to be performed:
Data Entry
Necessary Computations
Output Results
Help Support this site - Click this ad
Design the Graphical User Interface or GUI.
A TextBox to enter Quiz scores, txtQuizScore.
A ListBox to display the output, lstDisplay.
An Enter Button to signal the entry is complete, btnEnter.
A Display Button to request output of the data, btnDisplay.
Determine which controls respond to events.
The buttons respond to a click-event and do the necessary processing.
Design the logic of the event procedures.
Enter Button
Validate the data entered in the TextBox.
Score must be greater than 0.
Score must be less than 10.
Store the validated score in the Score array at the proper index.
Clear the TextBox.
Set the focus to the TextBox for the next Entry.
Make sure we do not exceed the size of the Array.
Display Button
Declare a loop variable to go through the indices.
Clear the ListBox.
Set Sum=0
Set Min=Score(0)
Set Max=Score(0)
Create a For Loop starting at 0 and proceeding to the last index.
Display the data entry.
Sum the Data Entry.
Test for Minimum
Text for Maximum
Determine Average
Make Sure Number of Entries >0
Average=Sum/Number of Entries.
Display Average, Minimum, Maximum
Translate the preceding pseudocode to Visual Basic.Net Code
Help Support this site - Click this ad
In order to continue this guide and finish Example #4 where we fullfill the
requirements, stated above in steps one through 5, press the
Button Below: