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:
Other People Are Reading
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.
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.
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.
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.
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") Return End If ItemArray(ArrayCountInteger) = TextBox1.Text ArrayCountInteger += 1 TextBox1.Clear() TextBox1.Focus() End If End Sub
This code reads each item that you enter in the TextBox and puts it into the array.
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 ListBox1.Items.Add(ItemArray(ListArrayCountInteger)) ListArrayCountInteger += 1 Else MessageBox.Show("No more items") Return End If Loop
This code reads the array using a loop and places the items into the ListBox for display.
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.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for