devxlogo

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        .Items.RemoveAt(fromIndex)        ' add the item        .Items.Insert(toIndex, data)        ' select the new item        .SelectedIndex = toIndex    End WithEnd Sub

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Five Early Architecture Decisions That Quietly Get Expensive

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.