Consulting

Results 1 to 3 of 3

Thread: How to change the position of item in the coulmn of listbox

  1. #1

    Question How to change the position of item in the coulmn of listbox

    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.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    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
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    thank you very mush for help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •