Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: VB7
Expertise: beginner
Apr 28, 2003



Building the Right Environment to Support AI, Machine Learning and Deep Learning

MoveListboxItem - Moving an item of a listbox to another index

' Moves an item of a listbox to another index
' If FROMINDEX = -1 then it moves the current highlighted item
' Example:
'   ' move up the selected item
'   MoveListboxItem(ListBox1, -1, ListBox1.SelectedIndex - 1)
'   ' move down the selected item
'   MoveListboxItem(ListBox1, -1, ListBox1.SelectedIndex + 1)

Sub MoveListboxItem(ByVal ctl As ListBox, ByVal fromIndex As Integer, _
    ByVal toIndex As Integer)
    If fromIndex = toIndex Then Exit Sub
    ' provide a default
    If fromIndex < 0 Then fromIndex = ctl.SelectedIndex
    ' exit if argument not in range
    If toIndex < 0 Or toIndex > ctl.Items.Count - 1 Then Exit Sub

    With ctl
        ' save the data of the current item
        Dim data As Object = .Items(fromIndex)
        ' remove the item
        ' add the item
        .Items.Insert(toIndex, data)
        ' select the new item
        .SelectedIndex = toIndex
    End With
End Sub
Marco Bellinaso
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date