Hot-Link Menu in Right-Side Column

Last Updated 6-30-2010

  • More Computers
  • More Games
  • More Electronics
  • More Books
  • More Software
  • Visual Basic Arrays Page 2

    1. Modify the label parameters as shown in the diagram below
      • Border Style ==> Fixed3D
      • Font ==> Courier New
      • (Name) ==> lblShow

      Example 3 Label

      Figure Array-4: Label control properties for Example 3

    Buy from Centurion

    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
    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.

    • Score (Index) = cint(Rnd () * 100):
      • Score(Index): Select an element of the array Score, by using Index for the position.
      • cint( ): cint which is shorthand for convert to integer converts the value inside the parentheses into the equivalent integer value.
      • Rnd( ): Rnd( ) is a built-in function in Visual Basic that generates a random number between 0 and 1.
      • *100: We need scores between 0 and 100, so we have to multiply the Rnd( ) values to fit into the specified range.

    • lblShow .Text &= Index .ToString + ". " + Score (Index).ToString +vbCrLf:
      • 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:

    Example 3 Run Time

    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.

    Help Support this site - Click this ad

    Listbox Control

    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:

    1. 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

    2. 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.

    3. Determine which controls respond to events.
      • The buttons respond to a click-event and do the necessary processing.

    4. Design the logic of the event procedures.
      • Enter Button
        1. Validate the data entered in the TextBox.
          • Score must be greater than 0.
          • Score must be less than 10.
        2. Store the validated score in the Score array at the proper index.
        3. Clear the TextBox.
        4. Set the focus to the TextBox for the next Entry.
        5. Make sure we do not exceed the size of the Array.
      • Display Button
        1. Declare a loop variable to go through the indices.
        2. Clear the ListBox.
        3. Set Sum=0
        4. Set Min=Score(0)
        5. Set Max=Score(0)
        6. 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
        7. Determine Average
          • Make Sure Number of Entries >0
          • Average=Sum/Number of Entries.
        8. Display Average, Minimum, Maximum

    5. 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:


    Visual Basic

    Introduction to VB.NET

    .NET Framework

    VS2008 IDE

    How VB is Compiled

    Start Visual Studio

    Windows Form App

    Save Your Work

    VB OOP Programming

    Visual Basic Code

    Exit Code

    Button Event Code

    Coding Recommendations


    Error List Window

    Comment Syntax

    Help Window

    Language Essentianl

    Built-In Data Types

    Declare Variables

    Declare Constants

    Code Arithmetic Expressions

    Assignment Statements

    Operator Precedence

    Type Casting

    Math Class

    String Declaration

    Conversion Functions

    Conversion Methods

    Formatting Functions

    String Formatting

    Variable Scope


    Nullable Types

    Loop Constructs

    For Next Loop

    Do While Loop

    Do Until Loop



    Exit Do | Exit For


    Nested Loops


    Array Declaration

    Rnd( ) Function

    Listbox Control


    Parallel Arrays

    Key Event Args

    Dynamic Arrays

    Redimension Array

    Set Breakpoint

    Start Debugger

    ReDim Preserve

    MultiDimensional Arrays

    DataGridView Control

    Length and Sort Methods


    Pad Right

    Split Method

    IsNumeric Function

    Multiform Projects

    Add Form To Project

    Form Object Methods

    Form Show Method

    ShowDialog Method

    Form Close Method

    Form Accept Button

    Multiform Project Example

    ASP.NET Web Programming

    Create Data Source

    Configure Access Data Source

    Add Product Class

    Extract Local Database Data

    Order PageLoad VB Code

    Add New Web Page

    Set Start Page

    Display Cart Aspx Code

    Display Cart Design View

    Sorted List Definition

    VB.NET Session State

    Create CartItem Class

    GetCartContents Function

    Add To Cart Event Handler

    Remove Cart Item Event

    Clear Cart Event Handler