How to move items up and down in listbox?

Moving items up and down in listbox. Here is my version of moving items up and down based on the references that I have collected and derive this code in vb.net.
‘moving up
            If Me.lbRoles.SelectedIndex > 0 Then
                Dim I = Me.lbRoles.SelectedIndex – 1
                Me.lbRoles.Items.Insert(I, Me.lbRoles.SelectedItem)
                Dim r = Me.lbRoles.SelectedIndex + 2
                Me.lbRoles.Items.RemoveAt(r)
                Me.lbRoles.SelectedIndex = I
            End If
‘moving down
            If Me.lbRoles.SelectedIndex < Me.lbRoles.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 = Me.lbRoles.SelectedIndex + 2
                Me.lbRoles.Items.Insert(I, Me.lbRoles.SelectedItem)
                Me.lbRoles.Items.RemoveAt(Me.lbRoles.SelectedIndex)
                Me.lbRoles.SelectedIndex = I – 1
            End If

Here are the other codes:
Dim item As Object = list.SelectedItem
If Not Item Is Nothing Then
Dim index As Integer = list.Items.IndexOf(item)
If (Not index = 0) Then
list.Items.RemoveAt(index)
index -= 1
list.Items.Insert(index, item)
list.SelectedIndex = index
End If
End If
‘down
Dim item As Object = list.SelectedItem
If Not Item Is Nothing Then
Dim index As Integer = list.Items.IndexOf(item)
If index < list.Items.Count – 1 Then
list.Items.RemoveAt(index)
index += 1
list.Items.Insert(index, item)
list.SelectedIndex = index
End If
End If
 ‘VB.NET move up
            lbRoles.Items.Insert(lbRoles.SelectedIndex + 1, lbRoles.Items(lbRoles.SelectedIndex – 1))
            lbRoles.Items.RemoveAt(lbRoles.SelectedIndex – 1)
 ‘C# move up
 listBox1.Items.Insert(listBox1.SelectedIndex+1,lis tBox1.Items[listBox1.SelectedIndex-1]);
 listBox1.Items.RemoveAt(listBox1.SelectedIndex-1);
 ‘C# move down
listBox1.Items.Insert(listBox1.SelectedIndex,listB ox1.Items[listBox1.SelectedIndex + 1]);
listBox1.Items.RemoveAt(listBox1.SelectedIndex+1);
 ‘VB.NET move down
            lbRoles.Items.Insert(lbRoles.SelectedIndex, lbRoles.Items(lbRoles.SelectedIndex + 1))
            lbRoles.Items.RemoveAt(lbRoles.SelectedIndex + 1)

Here are my references:

http://www.dreamincode.net/forums/topic/155775-move-items-up-and-down-in-listbox/
http://bytes.com/topic/visual-basic-net/answers/357262-listbox-how-do-i-move-selected-item-up-list-1-position
http://www.daniweb.com/forums/thread73217.html
http://www.xtremedotnettalk.com/showthread.php?t=91179
http://www.dotnetspark.com/kb/1922-wpf-move-updown-listboxitem-listbox.aspx

Advertisements
This entry was posted in VB.NET 2 AND ASP.NET 2. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s