Results 1 to 3 of 3

Thread: ListBox Transfer Query

  1. #1

    ListBox Transfer Query

    I am transferring items from ListBox2 to ListBox1. These list boxes are having 2 columns. Hence have to transfer them taking this in care.

    For a scenario when I have all the items in ListBox2 & select one by one item transfer from ListBox2 for transfer –

    Here I can successfully transfer selected item to ListBox1 & that is the first item of ListBox1 (since ListBox1 is empty). After transfer ListBox1 contains 1 item.
    When I attempt another item to transfer from ListBox2 to ListBox1, the earlier item is getting overwritten, by the newly transferred item.

    What can be the problem here?

    Thanks!




    [vba]With ListBox2
    For e = .ListCount - 1 To 0 Step -1
    If .Selected(e) = True Then ' if selected then
    If .List(e) = "DEMO_NAME" Then ' This code is for a member which is considered fixed in ListBox2
    MsgBox "Cannot transfer ListItem DEMO_NAME from ListBox2 to ListBox1."
    Else
    If ListBox1.ListCount = 0 Then
    ListBox1.AddItem .List(e), ListBox1.ListCount

    ListBox1.List(ListBox1.ListCount - 1, 0) = .List(e, 0)
    ListBox1.List(ListBox1.ListCount - 1, 1) = .List(e, 1)
    .RemoveItem e ' remove from ListBox2
    Else

    ListBox1.List(ListBox1.ListCount - 1, 0) = .List(e, 0) '.Column(ListBox2.ListCount - 1, 1) = .List(e, 1)
    ListBox1.List(ListBox1.ListCount - 1, 1) = .List(e, 1)
    .RemoveItem e ' remove from ListBox2
    End If
    End If
    End If
    Next
    End With
    [/vba]
    Last edited by Aussiebear; 11-13-2009 at 04:23 PM. Reason: Added VBA tags to code

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,445
    Location
    This seems to work better, but it moves all items even when selecting just one, which I cannot believe that you want

    [vba]

    Dim e
    With ListBox2

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

    If .Selected(e) = True Then

    If .List(e) = "DEMO_NAME" Then ' This code is for a member which is considered fixed in ListBox2

    MsgBox "Cannot transfer ListItem DEMO_NAME from ListBox2 to ListBox1."
    Else

    ListBox1.AddItem .List(e), ListBox1.ListCount

    ListBox1.List(ListBox1.ListCount - 1, 0) = .List(e, 0)
    ListBox1.List(ListBox1.ListCount - 1, 1) = .List(e, 1)
    .RemoveItem e ' remove from ListBox2
    End If
    End If
    Next

    .ListIndex = 0
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Nov 2009
    Posts
    1
    Location
    I believe this would do the trick

    [VBA]Private Sub cmdMoveLeft_Click()

    Dim lItem As Long
    Dim iIndex As Integer
    Dim iInd2 As Integer
    Dim x As Integer

    lItem = lbxSelected.ListIndex
    If lItem = -1 Then
    Exit Sub
    End If
    With lbxSelected
    x = lbxSelectable.ListCount
    iIndex = lbxSelected.ListIndex
    lbxSelectable.AddItem .List(iIndex, 0), x
    iInd2 = lbxSelectable.ListCount - 1
    lbxSelectable.List(iInd2, 1) = .List(iIndex, 1)
    End With
    With lbxSelected
    .RemoveItem lbxSelected.ListIndex
    End With


    End Sub[/VBA]

Posting Permissions

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