Solved

Moving items in a Listview object

Posted on 2004-03-27
5
452 Views
Last Modified: 2012-06-21
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!!

0
Comment
Question by:TheBOI
  • 3
5 Comments
 
LVL 15

Expert Comment

by:Timbo87
ID: 10697507
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
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 10697522
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;
0
 
LVL 15

Accepted Solution

by:
Timbo87 earned 80 total points
ID: 10697532
Oops, small problem in move down in C#. Here is the correct code:

Move Down:

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

Author Comment

by:TheBOI
ID: 10697611
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?
0
 
LVL 10

Expert Comment

by:EBatista
ID: 10700026
for listview the timbo's code is valid, just replace Listbox.SelectedIndex for ListView1.SelectedItem.Index
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question