1 Attachment(s)
[RESOLVED] Move Item in the listbox
How to move Item from the first index to second index one by one until last index. I have select the item. I want to move it down,just change the position. Code below does not works
Code:
For c = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(c) = True Then
Dim idx As Integer
idx = ListBox1.ListIndex
If ListBox1.ListIndex > 0 Then
ListBox1.ListIndex = idx - 1
End If
End If
Next
Re: Move Item in the listbox
Code:
Private Sub cmdUp_Click()
' only if the first item isn't the current one
If List1.ListIndex >= 0 Then
' add a duplicate item up in the listbox
List1.AddItem List1.Text, List1.ListIndex - 1
' make it the current item
List1.ListIndex = List1.ListIndex - 2
' delete the old occurrence of this item
List1.RemoveItem List1.ListIndex + 2
End If
End Sub
Private Sub cmdDown_Click()
' only if the last item isn't the current one
If List1.ListIndex <> -1 And List1.ListIndex < List1.ListCount - 1 Then
' add a duplicate item down in the listbox
List1.AddItem List1.Text, List1.ListIndex + 2
' make it the current item
List1.ListIndex = List1.ListIndex + 2
' delete the old occurrence of this item
List1.RemoveItem List1.ListIndex - 2
End If
End Sub
Re: Move Item in the listbox
Quote:
Originally Posted by Hack
Code:
Private Sub cmdUp_Click()
' only if the first item isn't the current one
If List1.ListIndex >= 0 Then
' add a duplicate item up in the listbox
List1.AddItem List1.Text, List1.ListIndex - 1
' make it the current item
List1.ListIndex = List1.ListIndex - 2
' delete the old occurrence of this item
List1.RemoveItem List1.ListIndex + 2
End If
End Sub
Highlighted line should be:
If List1.ListIndex > 0 Then
:)
Re: Move Item in the listbox
Also, the following is basically the same but with few variables (some times it makes it easier for some people to understand):
Code:
Private Sub Command1_Click()
Dim curItem$, curIndex%
' only if the first item isn't the current one
If List1.ListIndex > 0 Then
curIndex = List1.ListIndex
curItem = List1.List(curIndex)
List1.RemoveItem curIndex
List1.AddItem curItem, curIndex - 1
List1.Selected(curIndex - 1) = True
End If
End Sub
Re: Move Item in the listbox
Thank you Both of you... RhinoBull thank you for your correction.
Re: Move Item in the listbox
Quote:
Originally Posted by RhinoBull
Highlighted line should be:
If List1.ListIndex > 0 Then
:)
I KNEW THAT!!!!!!!!!!!!!!!!!!
I just wanted to see if you were paying attention. :D
(slinks off in embarassment)
Re: Move Item in the listbox
Quote:
Originally Posted by Hack
I KNEW THAT!!!!!!!!!!!!!!!!!!
:p :afrog:
(embarrassedly playing a wise guy)