Results 1 to 11 of 11

Thread: Remove duplicates from a combo box

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    14

    Remove duplicates from a combo box

    this is the code for the listbox
    ---------------------------------------------------------------------
    Dim i As Long
    Dim j As Long
    With List1
    For i = 0 To .ListCount - 1
    For j = .ListCount To (i + 1) Step -1
    If .List(j) = .List(i) Then
    .RemoveItem j
    End If
    Next
    Next
    End With

    End Sub


    ---------------------------------------------------------------------
    this is the best i can alter the code
    ---------------------------------------------------------------------

    Dim i As Long
    Dim j As Long
    With cboIUpREF
    For i = 0 To .Items.Count - 1
    For j = .Items.Count To (i + 1) Step -1
    If .Items.Item(j) = .Items.Item(i) Then
    .Items.Remove(j)
    End If
    Next
    Next
    End With


    the line "If .Items.Item(j) = .Items.Item(i) Then" causes an error, what could i change it to, or is there another way other removing dups from a combo box, im using vb.net.

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    The count property is not updated when you do a deletion while looping. So the outer loop will error out... make both loops a step down loop, start at list count end at 1.

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Since removing items changes ListCount, the only way to do what you want is to do it backward. In other words

    For i = .ListCount - 1 To 0 Step -1

    Would you be interested in simple code to prevent duplicates to start with?

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    14
    thanks for the reply, can you change the code with the new loop, im just starting out and im not sure what you mean

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    14
    iv just read your reply MartinLiss, to stop the duplicates from entering the file isnt possible becauseof the type of program

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    All you need to do is to change the first line of your code.

    VB Code:
    1. Dim i As Long
    2. Dim j As Long
    3. With List1
    4.     For i = .ListCount - 1 To 0 Step -1
    5.         For j = .ListCount To (i + 1) Step -1
    6.             If .List(j) = .List(i) Then
    7.                 .RemoveItem j
    8.             End If
    9.         Next
    10.     Next
    11. End With

    BTW you didn't answer my question. It's easy to prevent duplicates from being in the list to begin with. Do you want to know how?

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    14
    thanks for the code, yeah tell me how

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    14
    also im doing the code to remove dups from a combo box, the line List i have replaced with items.item but it doesent work, do you have any sugesstions,

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    VB Code:
    1. Option Explicit
    2. Private Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _
    3.                       (ByVal hWnd As Long, _
    4.                        ByVal wMsg As Long, _
    5.                        ByVal wParam As Long, _
    6.                        ByVal lParam As String) As Long
    7. Private Const CB_FINDSTRINGEXACT = &H158 ' If you use a ComboBox
    8. Private Const LB_FINDSTRINGEXACT = &H1A2
    9.  
    10. ' In a sub or function
    11.                 Dim lngRetVal As Long
    12.  
    13.                 lngRetVal = SendMessageString(List1.hWnd, _
    14.                                               LB_FINDSTRINGEXACT, -1&, _
    15.                                               "string to be added")
    16.                 If lngRetVal > -1& Then
    17.                     ' It's already in the list and lngRetVal is the listindex
    18.                 Else
    19.                     List1.Additem "string to be added"
    20.                 End If

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    14
    ok im working with the code youve just given me
    <VBCODE>lngRetVal = SendMessageString(cboIUpREF.hWnd, _</VBCODE>
    in vb.net its giving me a blue line under cboIUpREF.hWnd saying hWnd isnt a member of System.Windows.Forms.ComboBox, is there a different command for vb.net to use

  11. #11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width