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!!
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!!
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;
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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.Ind ex
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