Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Moving items in a Listview object

Posted on 2004-03-27
5
Medium Priority
?
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 320 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

610 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