Visual Basic Arrays Tutorial

Written by alan hughes
  • Share
  • Tweet
  • Share
  • Pin
  • Email
Visual Basic Arrays Tutorial
User Visual Basic's array functionality to build lists of items. (Stockbyte/Stockbyte/Getty Images)

Microsoft's Visual Basic allows for easy declaring and handling of single-element variables, but sometimes you need something more, like a table of elements with the same characteristics. You may want to loop through the table printing the elements, or you may want to build the table from a database. VB.NET facilitates this functionality through the use of arrays, including multidimensional arrays.

Skill level:


  1. 1

    Start Visual Basic and click the "New Project" link on the Start Page. Double-click "Windows Forms Application" to open a new form in the Visual Basic Design window.

  2. 2

    Scroll the cursor over the Toolbox tab to the left of the Design window to open the Toolbox menu. It may take a few seconds to load the ToolBox and display it.

  3. 3

    Double-click the "ListBox" menu item in the Common Controls section of the Toolbox to add a ListBox to your form. Double-click the "Button" menu item twice to add two Buttons to your form. Double-click the "TextBox" menu item to add a TextBox to your form. Drag the controls in the form to arrange Button 1 under the TextBox and arrange Button 2 under the ListBox.

  4. 4

    Double-click on the "Add Item" button to open the Visual Basic Code window. Click on the blank line under the "Public Class Form 1" statement at the top of the window and key the following line:

     Dim ItemArray(9) As String

    Dim ArrayCountInteger As Integer

    This statement declares an array of up to ten string-type items and an integer counter. The counter always begins at zero, so the "Dim" statement sets the number at nine, one less than the number of items in the array.

  5. 5

    Click on the blank line after the "Private Sub Button1_Click" statement and enter the following code so that the entire procedure with beginning and ending lines looks like this:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If TextBox1.Text > "" Then
            If ArrayCountInteger = 10 Then
                MessageBox.Show("You have entered the maximum number of items")
            End If
            ItemArray(ArrayCountInteger) = TextBox1.Text
            ArrayCountInteger += 1
        End If
    End Sub

    This code reads each item that you enter in the TextBox and puts it into the array.

  6. 6

    Display the array. Click on the Design tab to return to the Design window. Double-click on the "List Items" button to return to the Code window. Enter code between the two lines so that the final result looks like this:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim ListArrayCountInteger As Integer
        Do While ListArrayCountInteger < (ArrayCountInteger + 1)
            If ItemArray(ListArrayCountInteger) > "" Then
                ListArrayCountInteger += 1
                MessageBox.Show("No more items")
            End If

    This code reads the array using a loop and places the items into the ListBox for display.

  7. 7

    Click on the "Debug" menu item at the top of the screen and click on the "Start Debugging" link to test your program. Enter an item into the TextBox and click the "Add Item" button. Add several items and click the "List Items" button to see your items in the ListBox.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.