Jason88
09-06-2009, 03:11 PM
Hello, I'm using the code below to move udt items up or down. Does anybody know how to move udt items to the top or bottom?
The udt array is 0-based, but item 0 is never used. Each udt belongs to a Listview item and the Listview is 1-based.
Public Sub MoveUDTItemUp(cArray() As m_Items, ByVal Index As Long)
Dim udtSwap As m_Items
If Index < 2 Or Index > UBound(cArray) Then Exit Sub
udtSwap = cArray(Index)
cArray(Index) = cArray(Index - 1)
cArray(Index - 1) = udtSwap
End Sub
Public Sub MoveUDTItemDown(cArray() As m_Items, ByVal Index As Long)
Dim udtSwap As m_Items
If Index < 1 Or Index >= UBound(cArray) Then Exit Sub
udtSwap = cArray(Index)
cArray(Index) = cArray(Index + 1)
cArray(Index + 1) = udtSwap
End Sub
kassyopeia
09-06-2009, 04:21 PM
Something like
udtSwap = cArray(Index)
For Index = Index To 2 Step -1
cArray(Index) = cArray(Index - 1)
Next Index
cArray(1) = udtSwap
should do the trick (untested).
Jason88
09-06-2009, 06:25 PM
Thank you. Now I only need to find a way to move an item to the bottom. I thought this code would work, but it doesn't.
Public Sub MoveUDTItemBottom(cArray() As m_Items, ByVal Index As Long)
Dim udtSwap As m_Items
udtSwap = cArray(Index)
For Index = Index To UBound(cArray) - 1
cArray(Index) = cArray(Index + 1)
Next Index
cArray(UBound(cArray)) = udtSwap
End Sub
Jason88
09-09-2009, 05:02 AM
Does anybody know how to move an udt item to the bottom? I'm still looking for the answer.
My code above doesn't work.
kassyopeia
09-09-2009, 08:30 AM
Describing in what way it "doesn't work" would be helpful. ;)
mkaras
09-09-2009, 09:05 AM
Your code for moving to the bottom "looks" like it should be close.
Your problem may be the fact that you are using the entry argument to the subroutine (i.e. Index) as the For Loop iteration variable. In my way of thinking it is bad form to write code like this I would suggest instead that you declare a separate variable such as "I" to use as the For Loop Iterator.
mkaras
kassyopeia
09-09-2009, 09:35 AM
using the entry argument to the subroutine (i.e. Index) as the For Loop iteration variable.
That's really my "fault", since I suggested it. :o
I don't see how that can be the problem, though, if the *Top sub employs the same variable usage and works just fine...
Jason88
09-09-2009, 09:59 AM
It seems to work now.
I'm using the array of udt's in combination with a Listview. Each udt belongs to a Listview item. When items in the Listview are moved up and down the udt's need to be moved up and down as well.
When moving Listview items to the bottom they would not stay in the same order.
Item 1
Item 2
Item 3 <--
Item 4 <--
Item 5
would become
Item 1
Item 2
Item 5
Item 4 <-- (should be 3)
Item 3 <-- (should be 4)
I made some changes to the Listview "move to bottom" sub, so the moved items would stay in the same order. A had to make the same changes to the "move udt to bottom" sub for it to work properly.