473,372 Members | 851 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 1865

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

Similar topics

21
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
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
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
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
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
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
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
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
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.