473,503 Members | 11,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rearrange items in ListView

I'm currently writing a photo album sotware in C#.
I'm using a ListView component to diaplay thumbnail previews of the photos
and want to give the user the possibility to rearrange the photos via a
drag&drop operation. The problem is that it only appears to be possible to
rearrange items in a ListView if its view property is not set to
View.LargeIcons (which is the one I need to diaplay the thumbs). If I
rearrage the items without changing the view, the moved items are placed at
the end of the list and if I change the view to View.List before the
rearranging I can't change it back again.
Here's the code:
private void lvPhotos_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
// prevent lvPhotos from redrawing itself
lvPhotos.BeginUpdate();

// get the new location of the items
Point p = lvPhotos.PointToClient(new Point(e.X, e.Y));
int destIndex = lvPhotos.GetItemAt(p.X, p.Y).Index;

// create a new ListViewItems array
ListViewItem[] selItems = new ListViewItem[lvPhotos.SelectedItems.Count];
int[] selIndices = new int[lvPhotos.SelectedIndices.Count];

// create the listViewItems array that will be used instead of the
currently displayed one
ListViewItem[] newItems = new ListViewItem[lvPhotos.Items.Count];

for(int i = 0; i < selItems.Length; i++)
{
if(lvPhotos.SelectedIndices[i] == destIndex) // if any of the
selectedIndices is equal to the destIndex abort the operation
return;

// fill the array with the currently selected items
selItems[i] = (ListViewItem)
lvPhotos.Items[lvPhotos.SelectedIndices[i]].Clone();

// add the current selectedIndex to the selIndices array
selIndices[i] = lvPhotos.SelectedIndices[i];
}

// rearrange the photos in the album and break the operation if it can't
be saved
if(DoAlbumDragDrop(selIndices, destIndex) == false)
return;

// switch the lvPhotos view to List, since we can't edit the item
arrangement in LargeIcon view
lvPhotos.View = View.List;

// add the items to lvPhotos
for(int i = selItems.Length - 1; i >= 0; i--)
lvPhotos.Items.Insert(destIndex, selItems[i]);

// remove the old items from lvPhotos
foreach(ListViewItem item in lvPhotos.SelectedItems)
lvPhotos.Items.Remove(item);

// create a temp ImageList
ImageList ilTemp = new ImageList();
// fill the temp il with the photos in rearranged order
for(int i = 0; i < ilThumbs.Images.Count; i++)
{
if(i == destIndex)
{
int j;
for(j = selIndices.Length - 1; j >= 0; j--)
ilTemp.Images.Add(ilThumbs.Images[j]);
j = 0;
for(i = i; i < ilThumbs.Images.Count; i++)
{
if(i == selIndices[j])
j++;
else
ilTemp.Images.Add(ilThumbs.Images[i + j]);
}
break;
}
ilTemp.Images.Add(ilThumbs.Images[i]);
}
// replace the current il with ilTemp
ilThumbs.Images.Clear();
for(int i = 0; i < ilTemp.Images.Count; i++)
{
ilThumbs.Images.Add(ilTemp.Images[i]);
lvPhotos.Items[i].ImageIndex = i;
}

// reset the lvPhotos view
lvPhotos.View = View.LargeIcon;

// redraw lvPhotos
lvPhotos.EndUpdate();
lvPhotos.Refresh();
}

Thanks for your help in advance! :)

Jul 21 '05 #1
0 1882

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
5170
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
6
3385
by: Nick | last post by:
Hi there, I'm trying to implement drag-drop for my listview control in large icon view mode. Unfortunately the order of the items gets completely messed up upon inserting the item back into the...
0
417
by: tommazzo | last post by:
I'm currently writing a photo album sotware in C#. I'm using a ListView component to diaplay thumbnail previews of the photos and want to give the user the possibility to rearrange the photos via a...
5
10061
by: Phill W. | last post by:
(VB'2003) What's the correct way to remove multiple, selected items from a ListView control (say, from a ContextMenu)? I ask because I'm getting a very annoying ArgumentOutOfRangeException...
2
1278
by: laxwrestler27 | last post by:
Hello everyone, I'm creating a program for a courier service that takes info from a database to make a 2d array with the necessary info, then puts that on to an excel spreadsheet per delivery. ...
3
7630
by: RT | last post by:
Is there any way to make Listview items invisible or otherwise keep them from displaying? Seems like temporarily removing, then restoring later would be a severe runtime hit.
0
1876
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
2
6795
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
1
8330
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, In my application I need to periodically remove all current items from a ListView and add a new set into it. The following abbreviated code contains the basic idea: private void...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.