Solved

Moving items in a Listview object

Posted on 2004-03-27
5
451 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
for listview the timbo's code is valid, just replace Listbox.SelectedIndex for ListView1.SelectedItem.Index
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now