Hot-Link Menu in Right-Side Column

Last Updated 6-30-2010


 
MoreNotebooks.php
  • More Computers
  •  
    Morenintendo.php
  • More Games
  •  
    MoreprecisionMeasurement.php
  • More Electronics
  •  
    MoreComputerProgrammingBooks.php
  • More Books
  •  
    MoreOperatingSystems.php
  • More Software
  • Visual Basic Arrays Page 3

    The form at Design Time should look similar to the diagram below:

    Ex 4 Form

    Figure Array-7: Form at Design Time


    Buy from Centurion


    The form at design time with the controls expanded is shown below:

    Ex 4 Form Expanded

    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 (ByVal sender As System.Object , ByVal e As KeyPressEventArgs)
          Handles txtQuizScore.KeyPress
      • 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.
    • DataString = NumberOfScores .ToString + ". " + EnteredValue .ToString: Create string echoing number entered.
    • lstDisplay.Items.Add (DataString ): Visual Basic syntax to place data at the end of the ListBox display.


    • Private Sub btnClearLast_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearLast.Click.
      • Visual Basic generated code to respond to the Clear Last Entry button being pressed.

    • NumberOfScores -= 1: Reset Array index to last entry entered.
    • lstDisplay .Items .RemoveAt (NumberOfScores ): Remove the Last Entry.
    • txtQuizScore.Clear: Clear the data entry TextBox.
    • txtQuizScore.Focus: Set the cursor in the data entry TextBox.
    • Private Sub btnClearAll_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearAll.Click.
      • Visual Basic Generated code to respond to the Clear All button being pressed.

    • lstDisplay .Items .Clear: Syntax to clear a ListBox.
    • NumberOfScores=0: Effectively clear the array by resetting the index to 0.
    • txtQuizScore.Clear: Clear the data entry TextBox.
    • txtQuizScore.Focus: Set the cursor in the data entry TextBox.


    • Private Sub btnStatistics_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatistics.Click.
      • Visual Basic generated code to respond to the Statistics button being pressed.
    • Dim Output As String: Workhorse variable to prepare text to send out to ListBox display.
    • Dim Average As Double = 0: Preset the Average variable to 0 for clarity.
    • Dim Sum As Double = 0: The sum must start at 0, in order for the average calculation to work.
    • Dim ScoreNumber as Integer: Variable used to index through the Scores array.
    • Dim MinScore As Double = ScoreArray (0): Set the minimum value to the first value in the array.
    • Dim MinScore As Double = ScoreArray (0): Set the maximum value to the first value in the array.
    • lstDisplay .Items .Add (""): Add in a blank line in the ListBox display.
    • For ScoreNumber =0 to NumberOfScores - 1: Create For Loop to index through all Quiz Scores entered.
    • Sum+=ScoreArray (ScoreNumber): Sum each score in the array.
    • If(MinScore >ScoreArray (ScoreNumber) ) then: Check if the present score is lower than the previous minimum value.
    • MinScore = ScoreArray (ScoreNumber): Set New Minimum if lower.
    • If(MaxScore < ScoreArray (ScoreNumber)) then: Check if the present score is higher than the previous maximum value.
    • MaxScore = ScoreArray (ScoreNumber): Set the New Maximum if higher.
    • If(NumberOfScores>0 ) then: Check to make sure we don't get a /0 error.
    • Average = Sum/NumberOfScores: Calculate the average.
    • Output ="Average: " + Average .ToString: Create text to output Average.
    • lstDisplay .Items .Add (Output ): Display the Average.
    • Output ="Minimum: " + MinScore .ToString: Create and Display Minimum value.
    • 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
    5 Enter 1. 5
    6 Enter 1. 5
    2. 6
    7 Enter 1. 5
    2. 6
    3. 7
    8 Enter 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

    Ex 4 Run Time

    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:


    Help Support this site - Click this ad

    Home


    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

    If/Then/Else

    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

    Enumerations

    Nullable Types


    Loop Constructs

    For Next Loop

    Do While Loop


    Do Until Loop

    Do...Loop-While

    Do...Loop-Until


    Exit Do | Exit For

    Do...Loop


    Nested Loops


    Arrays

    Array Declaration


    Rnd( ) Function

    Listbox Control


    KeyPressEventArgs


    Parallel Arrays

    Key Event Args


    Dynamic Arrays

    Redimension Array

    Set Breakpoint

    Start Debugger


    ReDim Preserve


    MultiDimensional Arrays

    DataGridView Control


    Length and Sort Methods


    Structures

    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