Press "Enter" to skip to content

Add Items to ListBox, ComboBox in VB – Direct and from TextBox, another ListBox

VB: Adding Items to List Box or Combo Box

 Program Abstraction:       This article shows how to add items to listbox, combox box from another textbox or listbox.

ListBox and ComboBox are the major control elements on VB.net development. Basically both List box and combo box have same functions, but listbox shows all its items as a list whereas combo box shows only selected item and hide all other items.

Here let us see how to add items to list box or combo box using Visual Basic.
In both listbox and combobox, each elements are named as ‘item’.

Syntax: 

List Box:

     ListBox.items.add(itemname);

Combo Box:

ComboBox.items.add(itemname);

Example:

Here we create a list box and a combo box and a button will add items to the listbox or combobox.

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

ListBox1.Items.Add(“hello”)

ComboBox1.Items.Add(“hi”)

End Sub


Here the item is static and added by programmer.

VB Add items into listbox and combobox

Adding items from a Text Box to List box or Combo box:

Here user type an item name on the text box and a button will add text on the text box into to the list box or combo box.
Here we need a text box and button, button click event code will like this:

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

ListBox1.Items.Add(TextBox1.Text)

ComboBox1.Items.Add(TextBox1.Text)

End Sub


VB add textbox items to listbox combobox

Prevent Adding Null (Empty) values into List box or combobox:

Here we have to check the text on the textbox at first.
Item will be added to the list box or combo box if and only if the value on the text is not empty or null.

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

If (TextBox1.Text <> “”) Then

ListBox1.Items.Add(TextBox1.Text)

End If

End Sub

How to insert all List box items into a combo box or vice versa

There is no direct way to send all items from a list box to combo box. So have to insert each element one by one using a loop.
Loop start with first element (0th position) and end with list box item count.

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

Dim i As Integer

For i = 0 To ListBox1.Items.Count – 1

ComboBox1.Items.Add(ListBox1.Items(i))

Next

End Sub

Insert all list box items to combobox

we use loop limit as (ListBox1.Items.Count – 1) because list box item index started from 0th position.

How to show Combo Box first item as Combo Box selected text

ComboBox1.Text = ComboBox1.Items(0).ToString()

Showing combo box first item as selected
The Code ComboBox1.Items(0) takes first items (0th position) as its text. If we want to change its default value, then just give corresponding index value.

One Comment

  1. Honest Moyo Honest Moyo 17th September 2019

    Thank you so much, you saved my life

Leave a Reply