PDA

View Full Version : Listbox items getting transferred in reverse order



avadhutd2
10-14-2009, 06:23 AM
Hi,

Transfer of items between ListBoxes:

I am using the CTRL & SHIFT buttons to transfer the items of LIST BOX1 to LIST BOX2, but I can see them getting added in reverse order.

For example -

ListBox1
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7

Consider I use SHIFT & get the items 1 to 4 selected. When I transfer them to ListBox2, they get added in order as Item 4 is seen first & Item 1 is seen last.

Also, if I transfer the items back to ListBox 1 again they are in reverse order and appear at the end of existing items.

What should be done to appear the items in order as desired (may be alphabetical or as per ASC or DESC order of any column)?

Thanks !

Aflatoon
10-14-2009, 07:56 AM
How are you transferring them?

avadhutd2
10-14-2009, 11:01 PM
There is a button to transfer the selected items to the ListBox2 from ListBox1. The code for item transfer is as follows -

Private Sub Button1_Click()
Call Transfer(False) ‘ False here means transfer only selected ones
End Sub

' Definition
Private Sub Transfer(MoveAll As Boolean)

Dim e As Long
With ListBox1
If MoveAll = True Then
For e = 0 To .ListCount - 1
ListBox1.AddItem .List(e) 'move all items to Tobox
Next
.Clear
Else
For e = .ListCount - 1 To 0 Step -1
If .Selected(e) = True Then ' if selected then
ListBox1.AddItem .List(e) ' add to other list
.RemoveItem e ' remove
End If
Next
End If
End With
End Sub

Can you let me what is going wrong here?

Aflatoon
10-15-2009, 12:04 AM
The AddItem method allows you to specify where you want the item added. You also refer to Listbox1 rather than Listbox2 in the code! Try this change to your code:


Private Sub Transfer(MoveAll As Boolean)
Dim e As Long
With ListBox1
If MoveAll = True Then
For e = 0 To .ListCount - 1
ListBox2.AddItem .List(e) 'move all items to Tobox
Next
.Clear
Else
For e = .ListCount - 1 To 0 Step -1
If .Selected(e) = True Then ' if selected then
ListBox2.AddItem .List(e), 0 ' add to start of other list
.RemoveItem e ' remove
End If
Next
End If
End With
End Sub