Results 1 to 6 of 6

Thread: [RESOLVED] 2 listboxes

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Resolved [RESOLVED] 2 listboxes

    Hi
    I have 2 listboxes,I want to add the selected item of the listbox1 to listbox2 but if the item already exists in listbox2,I don't want to add it
    I used the following code but it didn't work.Why?
    For X = 1 To List2.ListCount
    If List2.List(X - 1) = List1.List(List1.ListIndex) Then
    Exit sub
    Else
    list2.additem list1.list(list1.listindex)
    End If
    Next

  2. #2
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Melbourne, Australia
    Posts
    415

    Re: 2 listboxes

    Try this
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim alreadyThere As Boolean
    3. alreadyThere = False
    4. For i = 0 To List2.ListCount - 1
    5.     If List2.List(i) = List1.List(List1.ListIndex) Then
    6.         alreadyThere = True
    7.     End If
    8. Next
    9. If Not alreadyThere Then
    10.     List2.AddItem List1.List(List1.ListIndex)
    11. End If
    12. End Sub

  3. #3
    Frenzied Member d3gerald's Avatar
    Join Date
    Jan 2006
    Posts
    1,348

    Re: 2 listboxes

    you might want to try it my way

    VB Code:
    1. Private Sub cmdAdd_Click()
    2.     Dim a As Long
    3.     For a = 0 To List2.ListCount - 1
    4.         If List2.List(a) = List1.Text Then
    5.             Exit Sub
    6.         End If
    7.     Next
    8.     List2.AddItem List1.Text
    9. End Sub
    On error goto Trap

    Trap:
    in case of emergency, drop the case...

    ****************************************
    If this post has been resolved. Please mark it as "Resolved" by going through the "Thread Tools" above and clicking on the "Mark Thread Resolved " option.
    if a post is helpful to you, Please Rate it by clicking on the Rate link right below the avatar

  4. #4
    Addicted Member iehjsucker's Avatar
    Join Date
    Sep 2005
    Posts
    150

    Re: 2 listboxes

    this code will go thru the listbox with just one click and no selection.

    dim L1Cnt as integer, L2Cnt as integer,i as integer, j as integer, prsnt as boolean
    L1Cnt = List1.listcount - 1
    L2Cnt = List2.listcount - 1

    for i = 0 to l1Cnt
    prsnt = false
    for j = 0 to l2cnt
    if list2.list(j) = list1.list(i) then
    prsnt = true
    end if
    next
    if prsnt = false
    list2.add list1.list(i)
    end if
    next





    =============
    ..code masters..

  5. #5
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: 2 listboxes

    And for the API approach.
    VB Code:
    1. Option Explicit
    2. Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" _
    3. (ByVal hWnd As Long, ByVal wMsg As Integer, ByVal wParam As String, _
    4. lParam As Any) As Long
    5.  
    6. Const LB_FINDSTRINGEXACT = &H1A2
    7.  
    8.  
    9. '-----------------------------------------------
    10. If SendMessage(List2.hWnd, LB_FINDSTRINGEXACT, -1, ByVal List1.List(List1.ListIndex)) = -1 Then
    11.    List2.AddItem List1.List(List1.ListIndex)
    12. End If
    You will also need to check that an item is actually selected in list1 with any of the codes.

    casey.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: 2 listboxes

    Hi
    thanks to you all

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