5 Replies - 19375 Views - Last Post: 25 February 2014 - 06:44 AM Rate Topic: -----

#1 gerbilplay4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-January 10

Move Items Up And Down In Listbox

Posted 13 February 2010 - 05:03 AM

Hello.
I have almost finished my media player, just need a spot of help. I want two buttons on my form, one to move the selected item in the listbox up and one to move the selected item in the listbox down.
I have no code what so ever with this and have no idea what to do, so I'm hoping you guys can at least point me in the right direction.

Thanks,
Jonathan.
Is This A Good Question/Topic? 0
  • +

Replies To: Move Items Up And Down In Listbox

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Move Items Up And Down In Listbox

Posted 13 February 2010 - 05:12 AM

A listbox has a collection of lines. Copy the lines out to a string array. Using the listbox's selectedindex your up/down buttons are manipulating, swap the string array's contents between the old and new positions in the listbox the selected index has been change to. Then copy the string array back to the items property.

See the example: ListBox.ObjectCollection..::.AddRange Method (array<Object>[]()[])

This post has been edited by n8wxs: 13 February 2010 - 05:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 Aeonhack  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 7
  • Joined: 25-January 10

Re: Move Items Up And Down In Listbox

Posted 13 February 2010 - 05:32 AM

    'Move up
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Make sure our item is not the first one on the list.
        If ListBox1.SelectedIndex > 0 Then
            Dim I = ListBox1.SelectedIndex - 1
            ListBox1.Items.Insert(I, ListBox1.SelectedItem)
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
            ListBox1.SelectedIndex = I
        End If
    End Sub
    
    'Move down
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Make sure our item is not the last one on the list.
        If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
           'Insert places items above the index you supply, since we want
           'to move it down the list we have to do + 2
            Dim I = ListBox1.SelectedIndex + 2
            ListBox1.Items.Insert(I, ListBox1.SelectedItem)
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
            ListBox1.SelectedIndex = I - 1
        End If
    End Sub


This post has been edited by Aeonhack: 13 February 2010 - 05:33 AM

Was This Post Helpful? 3
  • +
  • -

#4 gerbilplay4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-January 10

Re: Move Items Up And Down In Listbox

Posted 14 February 2010 - 01:25 AM

View PostAeonhack, on 13 February 2010 - 04:32 AM, said:

    'Move up
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Make sure our item is not the first one on the list.
        If ListBox1.SelectedIndex > 0 Then
            Dim I = ListBox1.SelectedIndex - 1
            ListBox1.Items.Insert(I, ListBox1.SelectedItem)
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
            ListBox1.SelectedIndex = I
        End If
    End Sub
    
    'Move down
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Make sure our item is not the last one on the list.
        If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
           'Insert places items above the index you supply, since we want
           'to move it down the list we have to do + 2
            Dim I = ListBox1.SelectedIndex + 2
            ListBox1.Items.Insert(I, ListBox1.SelectedItem)
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
            ListBox1.SelectedIndex = I - 1
        End If
    End Sub



Thank You so much, you are the best!
Was This Post Helpful? 0
  • +
  • -

#5 7umper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-April 12

Re: Move Items Up And Down In Listbox

Posted 29 April 2012 - 07:59 PM

View PostAeonhack, on 13 February 2010 - 05:32 AM, said:

    'Move up
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Make sure our item is not the first one on the list.
        If ListBox1.SelectedIndex > 0 Then
            Dim I = ListBox1.SelectedIndex - 1
            ListBox1.Items.Insert(I, ListBox1.SelectedItem)
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
            ListBox1.SelectedIndex = I
        End If
    End Sub
    
    'Move down
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Make sure our item is not the last one on the list.
        If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
           'Insert places items above the index you supply, since we want
           'to move it down the list we have to do + 2
            Dim I = ListBox1.SelectedIndex + 2
            ListBox1.Items.Insert(I, ListBox1.SelectedItem)
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
            ListBox1.SelectedIndex = I - 1
        End If
    End Sub



I also look for this, too. Thanks man
Was This Post Helpful? 0
  • +
  • -

#6 minh_11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-February 14

Re: Move Items Up And Down In Listbox

Posted 25 February 2014 - 06:44 AM

i tried to use this method but having an error message when trying to insert the object in the listbox,
it say that my "Operation is not valid while ItemsSource is in use....."
any idea how to get around this error ??

my ItemsSource is a New ObjectModel.ObservableCollection(Of object)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1