removing dups in combo box list

removing dups in combo box list

Post by Gordon Byrn » Thu, 14 Mar 2002 00:18:58



Hey I'm having a difficult time figuring out the algorythm for removing
duplicate values in a combo box, could anyone help me out?  I have arlready
loaded the combo box but now want to remove any dups.  Or should I not add
the dups as I load it, and if so, what is the algorythm for that?  I have
three combo boxes I want to clean up, so do can I do it at the same time or
one at a time?

thanks for any help.

Gordy

 
 
 

removing dups in combo box list

Post by Darry » Thu, 14 Mar 2002 02:44:11


Gordon,

try this:

For i = 0 To cboCtl.ListCount - 1
  For j = (i + 1) To cboCtl.ListCount - 1
   If cboCtl.List(j) = cboCtl.List(i) Then
    cboCtl.RemoveItem (j)
   End If
  Next j
 Next i

I haven't tested it under all conditions, but it should work for you.


Quote:> Hey I'm having a difficult time figuring out the algorythm for removing
> duplicate values in a combo box, could anyone help me out?  I have
arlready
> loaded the combo box but now want to remove any dups.  Or should I not add
> the dups as I load it, and if so, what is the algorythm for that?  I have
> three combo boxes I want to clean up, so do can I do it at the same time
or
> one at a time?

> thanks for any help.

> Gordy


 
 
 

removing dups in combo box list

Post by Jennif » Thu, 14 Mar 2002 06:45:21


Gordon,

I would not put duplicates in the combo box to start with.  Before you
add something, check to see if it is already there.  Here's some
simple code as an example.

HTH,
Jennifer

Private Sub Command1_Click()
    Dim sList(0 To 4) As String
    Dim X As Integer

    sList(0) = "apple"
    sList(1) = "orange"
    sList(2) = "pear"
    sList(3) = "apple"
    sList(4) = "lemon"

    For X = 0 To UBound(sList)
        If bInCombo(sList(X)) = False Then
            Combo1.AddItem sList(X)
        End If
    Next X

End Sub

Private Function bInCombo(sItem As String) As Boolean
    Dim X As Integer
    For X = 0 To Combo1.ListCount - 1
        If sItem = Combo1.List(X) Then
            bInCombo = True
            Exit Function
        End If
    Next X
    bInCombo = False
End Function


> Hey I'm having a difficult time figuring out the algorythm for removing
> duplicate values in a combo box, could anyone help me out?  I have arlready
> loaded the combo box but now want to remove any dups.  Or should I not add
> the dups as I load it, and if so, what is the algorythm for that?  I have
> three combo boxes I want to clean up, so do can I do it at the same time or
> one at a time?

> thanks for any help.

> Gordy

 
 
 

removing dups in combo box list

Post by Gordon Byrn » Thu, 14 Mar 2002 08:13:16


Jennifer, thank you for the input, it was just what I needed:)


> Gordon,

> I would not put duplicates in the combo box to start with.  Before you
> add something, check to see if it is already there.  Here's some
> simple code as an example.

> HTH,
> Jennifer

> Private Sub Command1_Click()
>     Dim sList(0 To 4) As String
>     Dim X As Integer

>     sList(0) = "apple"
>     sList(1) = "orange"
>     sList(2) = "pear"
>     sList(3) = "apple"
>     sList(4) = "lemon"

>     For X = 0 To UBound(sList)
>         If bInCombo(sList(X)) = False Then
>             Combo1.AddItem sList(X)
>         End If
>     Next X

> End Sub

> Private Function bInCombo(sItem As String) As Boolean
>     Dim X As Integer
>     For X = 0 To Combo1.ListCount - 1
>         If sItem = Combo1.List(X) Then
>             bInCombo = True
>             Exit Function
>         End If
>     Next X
>     bInCombo = False
> End Function




- Show quoted text -

Quote:> > Hey I'm having a difficult time figuring out the algorythm for removing
> > duplicate values in a combo box, could anyone help me out?  I have
arlready
> > loaded the combo box but now want to remove any dups.  Or should I not
add
> > the dups as I load it, and if so, what is the algorythm for that?  I
have
> > three combo boxes I want to clean up, so do can I do it at the same time
or
> > one at a time?

> > thanks for any help.

> > Gordy

 
 
 

removing dups in combo box list

Post by Randy Birc » Thu, 14 Mar 2002 08:31:58


Gord ...

Instead of removing dupes, why not not add them in the first place.  Try
this ...

Private Const LB_FINDSTRINGEXACT = &H1A2

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long

Private Sub AddToUniqueList(sListItem As String)

   If SendMessage(List1.hwnd, _
                  LB_FINDSTRINGEXACT, _
                  -1, _
                  ByVal sListItem) = -1 Then

      List1.AddItem sListItem

   End If

End Sub

Private Sub Command1_Click()

   AddToUniqueList "apple"
   AddToUniqueList "apple"
   AddToUniqueList "pear"
   AddToUniqueList "apple"
   AddToUniqueList "lemon"
   AddToUniqueList "apple"
   AddToUniqueList "orange"
   AddToUniqueList "orange"

End Sub

If you need to add itemdata as well, just pass as an additional parameter
...

Private Sub AddToUniqueList(sListItem As String, nValue As Long)

   If SendMessage(List1.hwnd, _
                  LB_FINDSTRINGEXACT, _
                  -1, _
                  ByVal sListItem) = -1 Then

      List1.AddItem sListItem
      List1.ItemData(List1.NewIndex) = nValue

   End If

End Sub

Private Sub Command1_Click()

   Call AddToUniqueList("apple", 123)
   Call AddToUniqueList("apple", 123)
   Call AddToUniqueList("pear", 234)
   Call AddToUniqueList("apple", 123)
   Call AddToUniqueList("lemon", 555)
   Call AddToUniqueList("apple", 123)
   Call AddToUniqueList("orange", 898)
   Call AddToUniqueList("orange", 898)

End Sub

--

Randy Birch
MVP Visual Basic

http://www.mvps.org/vbnet/

Please respond only to the newsgroups so all can benefit.

*** If you call the Sleep API in Bill's latest, will it have .NET dreams?
***


> Jennifer, thank you for the input, it was just what I needed:)



> > Gordon,

> > I would not put duplicates in the combo box to start with.  Before you
> > add something, check to see if it is already there.  Here's some
> > simple code as an example.

> > HTH,
> > Jennifer

> > Private Sub Command1_Click()
> >     Dim sList(0 To 4) As String
> >     Dim X As Integer

> >     sList(0) = "apple"
> >     sList(1) = "orange"
> >     sList(2) = "pear"
> >     sList(3) = "apple"
> >     sList(4) = "lemon"

> >     For X = 0 To UBound(sList)
> >         If bInCombo(sList(X)) = False Then
> >             Combo1.AddItem sList(X)
> >         End If
> >     Next X

> > End Sub

> > Private Function bInCombo(sItem As String) As Boolean
> >     Dim X As Integer
> >     For X = 0 To Combo1.ListCount - 1
> >         If sItem = Combo1.List(X) Then
> >             bInCombo = True
> >             Exit Function
> >         End If
> >     Next X
> >     bInCombo = False
> > End Function



> > > Hey I'm having a difficult time figuring out the algorythm for
removing
> > > duplicate values in a combo box, could anyone help me out?  I have
> arlready
> > > loaded the combo box but now want to remove any dups.  Or should I not
> add
> > > the dups as I load it, and if so, what is the algorythm for that?  I
> have
> > > three combo boxes I want to clean up, so do can I do it at the same
time
> or
> > > one at a time?

> > > thanks for any help.

> > > Gordy

 
 
 

removing dups in combo box list

Post by David Segal » Thu, 14 Mar 2002 19:35:06


Posted and emailed:


>Hey I'm having a difficult time figuring out the algorythm for removing
>duplicate values in a combo box, could anyone help me out?  I have arlready
>loaded the combo box but now want to remove any dups.  Or should I not add
>the dups as I load it, and if so, what is the algorythm for that?  I have
>three combo boxes I want to clean up, so do can I do it at the same time or
>one at a time?

>thanks for any help.

>Gordy

Assuming that the .Sorted property of your combo box is true the
easiest way is to add the item to the combo box and then remove the
item if it is equal to the previous item or the next next item.
.NewIndex contains the location of the current item and, of course,
you should check that .NewIndex -1 or .NewIndex + 1 exist before you
attempt the comparison.
 
 
 

removing dups in combo box list

Post by Joe » Fri, 15 Mar 2002 09:48:48



> Hey I'm having a difficult time figuring out the algorythm for removing
> duplicate values in a combo box, could anyone help me out?  I have arlready
> loaded the combo box but now want to remove any dups.  Or should I not add
> the dups as I load it, and if so, what is the algorythm for that?  I have
> three combo boxes I want to clean up, so do can I do it at the same time or
> one at a time?

How many items are we talking about?  Does uppercase/lowercase matter?

--
Joe Foster <mailto:jlfoster%40znet.com>  KrazyKookKultz! <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!

 
 
 

1. Combo box Q: - whether to display the list box

I have a simple enough question, but answer eludes me. I have a combo
box in my dialog box. When the user clicks the down arrow, the combo box
sends a CBN_DROPDOWN message _prior_ to displaying the drop down list
box portion of the combo.

My CBN_DROPDOWN routine decides whether to allow the list box to be
displayed or not. The problem is how do I inform the combo box about
what the routine decides? Simply returning TRUE or FALSE makes no
difference. Any ideas?

--
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

www.csis.gvsu.edu/~ensinkb                          ICQ: 24871602

2. BBC Basic V to ASCII text convertor???

3. Modify the style of list box of a drop-down combo box?

4. X2 BBS

5. MFC: What is the limit on items in a Combo box and List Box?

6. Help to start a drawing Program

7. Combo box in List box

8. A question on frequency against time mapping

9. Wider list box of a combo box

10. Need help Removing right-click menu from drop down combo box in Visual Basic 5.0

11. Dropdown list is not appearing in combo box

12. Combo Box Directory Listing MFC / VS .net / XP