How to Read Lines From a Listbox in Visual Basic
It's a good idea to use a list box control when you need the user to select multiple lines in your Visual Basic application. List boxes display a list of items that you can select one or more items from. In VB, you can use the "Items" property to add or get items from a list box control. The property enables you to obtain a reference to the list of items, which you can use to read the lines in the control.
Instructions
-
-
1
Launch Microsoft Visual Basic Express, click "New Project" on the left pane of your computer screen and double-click "Windows Forms Application" to create a new project.
-
2
Double-click "ListBox" from the "Toolbox" pane to add a new list box control on your form. Double-click "Button" to create a new button on your form.
-
-
3
Double-click the form to create a new Load event procedure and add the following code to add items to the list box control:
Me.ListBox1.Items.Add("Line1")
Me.ListBox1.Items.Add("Line2")
Me.ListBox1.Items.Add("Line3")
Me.ListBox1.Items.Add("Line4")
Me.ListBox1.Items.Add("Line5")
-
4
Double-click the button to create a new click event procedure and add the following code to read all the lines from the list box control:
For Each item In Me.ListBox1.Items
MessageBox.Show(item.ToString)
Next
-
5
Press "F5" to run the program and click "Button1" to display a message box for each line in the list box.
-
1