yn19832
03-22-2007, 10:15 AM
I have built a listbox named ListCT, and I want to move up the selected item with the "Move up" Button. The list box contain two columns and the codes are as following:
Private Sub UpButton_Click()
If LiqForm.ListCT.ListIndex <= 0 Then Exit Sub
NumItems = LiqForm.ListCT.ListCount
Dim Templist()
ReDim Templist(0 To NumItems - 1, 0 To 1)
'Selected Item
Item = LiqForm.ListCT.ListIndex
'Fill Array with list box items
For i = 0 To NumItems - 1
Templist(i, 0) = LiqForm.ListCT.List(i, 0)
Templist(i, 1) = LiqForm.ListCT.List(i, 1)
Next i
'Exchange Item
TempItem0 = Templist(Item, 0)
TempItem1 = Templist(Item, 1)
Templist(Item, 0) = Templist(Item - 1, 0)
Templist(Item, 1) = Templist(Item - 1, 1)
Templist(Item - 1, 0) = TempItem0
Templist(Item - 1, 1) = TempItem1
LiqForm.ListCT.List(i, 0) = Templist(i, 0)
LiqForm.ListCT.List(i, 1) = Templist(i, 1)
'Change the list index
LiqForm.ListCT.ListIndex = Item - 1
End Sub
But when I run the codes, the following error comes out
Subscript out of range (Error 9)
When I debug, it comes to LiqForm.ListCT.List(i, 0) = Templist(i, 0)
Can anyone help me sort it out?
Private Sub UpButton_Click()
If LiqForm.ListCT.ListIndex <= 0 Then Exit Sub
NumItems = LiqForm.ListCT.ListCount
Dim Templist()
ReDim Templist(0 To NumItems - 1, 0 To 1)
'Selected Item
Item = LiqForm.ListCT.ListIndex
'Fill Array with list box items
For i = 0 To NumItems - 1
Templist(i, 0) = LiqForm.ListCT.List(i, 0)
Templist(i, 1) = LiqForm.ListCT.List(i, 1)
Next i
'Exchange Item
TempItem0 = Templist(Item, 0)
TempItem1 = Templist(Item, 1)
Templist(Item, 0) = Templist(Item - 1, 0)
Templist(Item, 1) = Templist(Item - 1, 1)
Templist(Item - 1, 0) = TempItem0
Templist(Item - 1, 1) = TempItem1
LiqForm.ListCT.List(i, 0) = Templist(i, 0)
LiqForm.ListCT.List(i, 1) = Templist(i, 1)
'Change the list index
LiqForm.ListCT.ListIndex = Item - 1
End Sub
But when I run the codes, the following error comes out
Subscript out of range (Error 9)
When I debug, it comes to LiqForm.ListCT.List(i, 0) = Templist(i, 0)
Can anyone help me sort it out?