Copying item from LisBox 1 to ListBox 2 without duplicate?

Discussion in 'AutoCAD' started by pkirill, Apr 21, 2004.

  1. pkirill

    pkirill Guest

    I have a long list of blocks in ListBox1. The user can select blocks to add
    to ListBox2 by double clicking an itme in ListBox1. How can I prevent the
    item from being added if it already exists in ListBox2? Here is the
    senseless jumble I have been trying:

    Private Sub list1_dblclick(ByVal Cancel As MSForms.ReturnBoolean)
    Dim strListText As String
    Dim AddItem As Boolean
    Dim strlistitem(500) As String

    Dim i As Integer

    strListText = List1.Text
    For i = 0 To List2.ListCount - 1

    If strListText = strlistitem(i) Then
    AddItem = False
    End If
    Next

    If Not AddItem = False Or List2.ListCount = 0 Then
    List2.AddItem strListText

    End If
    End Sub

    Any help is always appreciated! Thanks!
     
    pkirill, Apr 21, 2004
    #1
  2. pkirill

    pkirill Guest

    So I think I got it. Any reason why the code below might fail me?

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Private Sub list1_dblclick(ByVal Cancel As MSForms.ReturnBoolean)
    Dim strList2Text(500) As String
    Dim strList1Text As String
    Dim AddItem As Boolean


    Dim i As Integer

    strList1Text = List1.Text
    For i = 0 To List2.ListCount - 1
    strList2Text(i) = List2.List(i)
    If strList1Text = strList2Text(i) Then
    AddItem = False
    GoTo EndofSub
    Else: AddItem = True
    End If
    Next

    If AddItem = True Or List2.ListCount = 0 Then
    List2.AddItem strList1Text

    End If

    EndofSub:

    End Sub
     
    pkirill, Apr 21, 2004
    #2
  3. pkirill

    Mark Propst Guest

    is this a typo? :))
     
    Mark Propst, Apr 21, 2004
    #3
  4. pkirill

    pkirill Guest

    Nope, that's what happens when you put your 'else' item on the same line...
     
    pkirill, Apr 21, 2004
    #4
  5. Any reason why the code below might fail me?
    No, your logic seemed fine. The only unnecessary limitation I saw was that
    your code would choke (unnecessarily) for a listbox with > 500 items. I
    couldn't resist doing a little streamlining while I was looking at it;
    delete the comments to see it.

    James


    Private Sub list1_dblclick(ByVal Cancel As MSForms.ReturnBoolean)
    'Dim strList2Text(500) As String
    Dim strList1Text As String
    Dim AddItem As Boolean
    Dim i As Integer

    AddItem = True 'assume no match until one is found
    strList1Text = List1.Text
    For i = 0 To List2.ListCount - 1
    'strList2Text(i) = List2.List(i)
    'array is not helping you in this instance...
    If strList1Text = List2.List(i) Then
    AddItem = False
    'GoTo EndofSub
    Exit For
    'Else: AddItem = True
    ' AddItem keeps its True value until a match is found
    End If
    Next
    ' if Or List2.ListCount = 0 then For loop will be skipped
    ' i.e. For i = 0 to -1 will not execute

    If AddItem = True Then List2.AddItem strList1Text
    'End If

    End Sub
     
    James Belshan, Apr 21, 2004
    #5
  6. pkirill

    Mark Propst Guest

    Cool, i never tried that!

    line...
     
    Mark Propst, Apr 21, 2004
    #6
  7. pkirill

    pkirill Guest

    James -

    Thanks for that. I figured out the not needing an array thing - but your
    code is much cleaner and to the point. Much appreciated! I often set my
    arrays to a high number because I keep getting "subscript out of range"
    errors when using dynamic arrays. I have the hardest time trouble shooting
    those! It's a bad habit I need to break..
     
    pkirill, Apr 22, 2004
    #7
  8. pkirill

    Mark Propst Guest

    just an aside...
    after messing with arrays for a while and seeing what a pain they are,
    then starting to use collections anywhere you would think to use an array, i
    found
    they are soooooo much easier to use, i'll never use an array again (unless i
    have to - like when they're required for input to a function or something)

    Mark
     
    Mark Propst, Apr 22, 2004
    #8
  9. pkirill

    bcoward Guest

    Just for future reference:

    If you just want to delete duplicates in a listbox here is some basic code. I use this in a condition where long filenames are present so there aren't a lot of items to iterate.

    Hope it helps for those little lists.

    Private Sub RemoveListBxDuplicates(MyList As ListBox)

    Dim I As Integer
    Dim J As Integer
    Dim lstItem As String
    Dim CmparedItem As String

    For I = 0 To MyList.ListCount - 1
    lstItem = MyList.List(I)
    For J = (I + 1) To MyList.ListCount
    CmparedItem = MyList.List(J)
    If lstItem = CmparedItem Then
    MyList.RemoveItem (J)
    J = J - 1
    End If
    Next J
    Next I

    End Sub


    Best of luck,

    Bob Coward
    CADS, Inc

    800-366-0946
     
    bcoward, Apr 23, 2004
    #9
  10. pkirill

    wivory Guest

    Hmmm...be careful here. I *think* that MyList.ListCount would only get evaluated at the *initiation* of the loop (not at each iteration), and if you're removing items as you go you could end up doing too many loops! I am hesitant because Bob says he has used this code already, but perhaps he culled it from a bigger example and something has been lost in the process.

    Regards

    Wayne Ivory
    IT Analyst Programmer
    Wespine Industries Pty Ltd
     
    wivory, Apr 27, 2004
    #10
  11. pkirill

    wivory Guest

    I forgot to add that if I'm correct, you can replace the For loop with a Do-While loop which *does* evaluate the condition at each iteration.

    Regards

    Wayne
     
    wivory, Apr 27, 2004
    #11
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.