Programming for Everybody: How to avoid adding duplicate items in a ComboBox in VB ?

How to avoid adding duplicate items in a ComboBox in VB ?

Dim conn9 As New OleDbConnection
            conn9.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\store1.mdb;Jet OLEDB:Database Password=123456")
            conn9.Open()
            Dim strsql9 As String
            strsql9 = "select * from types where code1='" + TextBox6.Text + "' "
            Dim cmd9 As New OleDbCommand(strsql9, conn9)
            myreader9 = cmd9.ExecuteReader
            ComboBox7.Items.Clear()
          

            While myreader9.Read()
                ComboBox7.Items.Add(myreader9("n1"))
                             Dim i, j As Int32
                For i = 0 To Me.ComboBox7.Items.Count - 2 Step 1
                    For j = Me.ComboBox7.Items.Count - 1 To i + 1 Step -1
                        If Me.ComboBox7.Items(i).ToString = Me.ComboBox7.Items(j).ToString Then
                            Me.ComboBox7.Items.RemoveAt(j)

                        End If
                    Next
                Next

                Dim i1, j1 As Int32
                For i1 = 0 To Me.ComboBox8.Items.Count - 2 Step 1
                    For j1 = Me.ComboBox8.Items.Count - 1 To i1 + 1 Step -1
                        If Me.ComboBox8.Items(i1).ToString = Me.ComboBox8.Items(j1).ToString Then
                            Me.ComboBox8.Items.RemoveAt(j1)

                        End If
                    Next
                Next
            End While

        Catch


        End Try



      

No comments:

Post a Comment