Adding Items in a ListBox

In Visual Basic.Net, items can also be added at runtime using the Add( ) method. We can apply this method in Visual Basic 2017, Visual Basic 2015, Visual Basic 2013, Visual Basic 2012, Visual Basic 2010 as well as Visual Basic 2008.

The syntax of the Add() method is as follows:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 ListBox1.Items.Add(text)
End Sub

Example:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     ListBox1.Items.Add(“Apple”)
End Sub

The Output is as shown below:

 

Besides that, we can also allow the user to add items via a popup input box.

In the following example, we create a variable myitem and then assign a value to myitem via the InputBox function that store the input from the user. We then use the Add() method to add the user’s item into the listbox. The code is as follows:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myitem                        'declare the variable myitem 
 myitem = InputBox("Enter your Item")
ListBox1.Items.Add(myitem)
End Sub

For more examples, refer to
http://www.vbtutor.net/vb2017/VB2017_Lesson6.html