Technology Programming

How to Remove Combo Box Duplicates

Things You'll Need

Instructions

1

Start Microsoft Visual Basic Express, click "New Project..." on the left pane of your screen and then select "Windows Forms Application." Click "OK."
2

Double click "Button1" to open the "Form1.vb"module. Type the following above "End Class" to load items to the combo box during Form load:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.ComboBox1.Items.Add("Line1")

Me.ComboBox1.Items.Add("Line1")

Me.ComboBox1.Items.Add("Line1")

Me.ComboBox1.Items.Add("Line2")

Me.ComboBox1.Items.Add("Line3")

Me.ComboBox1.Items.Add("Line3")

Me.ComboBox1.Items.Add("Line4")

Me.ComboBox1.Items.Add("Line4")

Me.ComboBox1.Items.Add("Line5")

End Sub
3

Type the following under "Button1_Click" to remove duplicates when user clicks "Button1":

Dim iCnt As Integer

Dim jCnt As Integer

For iCnt = 0 To Me.ComboBox1.Items.Count - 2

For jCnt = Me.ComboBox1.Items.Count - 1 To iCnt + 1 Step -1
If Me.ComboBox1.Items(iCnt).ToString = Me.ComboBox1.Items(jCnt).ToString Then

Me.ComboBox1.Items.RemoveAt(jCnt)

End If

Next

Next
4

Press "F5" to execute the program. Click the combo-box control to view duplicate items. Click "Button1" to remove duplicate items.

Leave a reply