Link to home
Start Free TrialLog in
Avatar of TheBOI
TheBOI

asked on

Moving items in a Listview object

Greetings all

I have a problem here which involves a ListView object, filled with ListViewItems. This may be a very easy problem for some of you guys on here...

What i need is a way to move the selected ListViewItem up and down in the ListView object.

I have two buttons, marked "Move up", and "Move down".

Some help would be very cool

Tchau!!

Avatar of Timbo87
Timbo87

Here's an example using ListBox1.

Move Up:

Dim temp As Object
Dim i As Integer = ListBox1.SelectedIndex
If i - 1 < 0 Then
    Exit Sub
End If
temp = ListBox1.Items(i)
ListBox1.Items(i) = ListBox1.Items(i - 1)
ListBox1.Items(i - 1) = temp
ListBox1.SelectedIndex = i - 1

Move Down:

Dim temp As Object
Dim i As Integer = ListBox1.SelectedIndex
If i + 1 > ListBox1.Items.Count - 1 Then
    Exit Sub
End If
temp = ListBox1.Items(i)
ListBox1.Items(i) = ListBox1.Items(i + 1)
ListBox1.Items(i + 1) = temp
ListBox1.SelectedIndex = i + 1
C#

Move Up:

object temp;
int i = ListBox1.SelectedIndex;
if(i - 1 < 0)
       return;
temp = ListBox1.Items[i];
ListBox1.Items[i] = ListBox1.Items[i - 1];
ListBox1.Items[i - 1] = temp;
ListBox1.SelectedIndex = i - 1;

Move Down:

object temp;
int i = ListBox1.SelectedIndex;
if(i + 1 < 0)
      return;
temp = ListBox1.Items[i];
ListBox1.Items[i] = ListBox1.Items[i + 1];
ListBox1.Items[i + 1] = temp;
ListBox1.SelectedIndex = i + 1;
ASKER CERTIFIED SOLUTION
Avatar of Timbo87
Timbo87

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TheBOI

ASKER

Ah ok, thats great, i understand how that works.  I really should have specified that platform i am using.  

The problem is i am using VB.Net, and the control is a Listview object, not a Listbox (although i am aware they are similar).

Any chance you know how to do this for a Listview?
for listview the timbo's code is valid, just replace Listbox.SelectedIndex for ListView1.SelectedItem.Index