PDA

View Full Version : ListBox Transfer Query



avadhutd2
11-13-2009, 05:21 AM
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!




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

Bob Phillips
11-13-2009, 05:47 AM
This seems to work better, but it moves all items even when selecting just one, which I cannot believe that you want



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

Marduc
11-20-2009, 08:39 AM
I believe this would do the trick

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