PDA

View Full Version : How to change the position of item in the coulmn of listbox



Nader
01-18-2008, 04:06 AM
I use this code to add item into listbox and remove item into listbox
ListBox2.AddItem TextBox1.Text
ListBox2.ListIndex = ListBox2.ListCount - 1

ListBox2.RemoveItem ListBox2.ListIndex
ListBox2.ListIndex = ListBox2.ListCount - 1

I want to move the item I inserted in the list to below or to above. it's mean change the position of them in the list by using two command button.

Bob Phillips
01-18-2008, 05:18 AM
Private Sub cmdUp_Click()
Dim SelectedValue As Variant
Dim tmpValues As Variant
Dim i As Long

With Me.ListBox2

If .ListIndex <> -1 And .ListIndex <> 0 Then

ReDim tmpValues(0 To .ListCount - 1)
SelectedValue = .Value
For i = 0 To .ListIndex - 2

tmpValues(i) = .List(i)
Next i

tmpValues(i) = SelectedValue

tmpValues(i + 1) = .List(.ListIndex - 1)
For i = .ListIndex + 1 To .ListCount - 1

tmpValues(i) = .List(i)
Next i
.List = tmpValues
End If
End With
End Sub
Private Sub cmdDown_Click()
Dim SelectedValue As Variant
Dim tmpValues As Variant
Dim i As Long

With Me.ListBox2

If .ListIndex <> -1 And .ListIndex <> .ListCount - 1 Then

ReDim tmpValues(0 To .ListCount - 1)
SelectedValue = .Value
For i = 0 To .ListIndex - 1

tmpValues(i) = .List(i)
Next i

tmpValues(i) = .List(.ListIndex + 1)
tmpValues(i + 1) = SelectedValue

For i = .ListIndex + 2 To .ListCount - 1

tmpValues(i) = .List(i)
Next i
.List = tmpValues
End If
End With
End Sub

Nader
01-18-2008, 06:13 AM
thank you very mush for help