473,657 Members | 2,776 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_DragDr op(object sender,
System.Windows. Forms.DragEvent Args e)
{
// prevent lvPhotos from redrawing itself
lvPhotos.BeginU pdate();

// get the new location of the items
Point p = lvPhotos.PointT oClient(new Point(e.X, e.Y));
int destIndex = lvPhotos.GetIte mAt(p.X, p.Y).Index;

// create a new ListViewItems array
ListViewItem[] selItems = new ListViewItem[lvPhotos.Select edItems.Count];
int[] selIndices = new int[lvPhotos.Select edIndices.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.Sel ectedIndices[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.Select edIndices[i]].Clone();

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

// rearrange the photos in the album and break the operation if it can't
be saved
if(DoAlbumDragD rop(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(destInde x, selItems[i]);

// remove the old items from lvPhotos
foreach(ListVie wItem item in lvPhotos.Select edItems)
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.Leng th - 1; j >= 0; j--)
ilTemp.Images.A dd(ilThumbs.Ima ges[j]);
j = 0;
for(i = i; i < ilThumbs.Images .Count; i++)
{
if(i == selIndices[j])
j++;
else
ilTemp.Images.A dd(ilThumbs.Ima ges[i + j]);
}
break;
}
ilTemp.Images.A dd(ilThumbs.Ima ges[i]);
}
// replace the current il with ilTemp
ilThumbs.Images .Clear();
for(int i = 0; i < ilTemp.Images.C ount; i++)
{
ilThumbs.Images .Add(ilTemp.Ima ges[i]);
lvPhotos.Items[i].ImageIndex = i;
}

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

// redraw lvPhotos
lvPhotos.EndUpd ate();
lvPhotos.Refres h();
}

Thanks for your help in advance! :)

Jul 21 '05 #1
0 1909

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

Similar topics

21
5197
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 table and 11 sub items with data about each column. When a Row in ListView1 is selected the Data in ListVies2 is loaded to show the correct data. Initially the first row in ListView1 is selected in FormX load
6
3400
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 listview, removing the item obviously keeps everything fine but the second I use insert the item is added to the *end* of the listview. Does anyone have any tips on how I can solve this issue?? Call Items.Remove(SourceItem)
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 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...
5
10076
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 because the ListView seems to be trying to "re-select" items that are no longer there! for example, giventhat I have 3 items in my list: Select the first and remove it - no problem.
2
1285
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. I have already finished coding that part and it works like a charm. What I would like to do is to have the user be able to rearrange the items on the screen in the program window and then have that order rearrange the 2d array. How can I put...
3
7641
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
1886
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 form every thing works fine: checking items by code as well as checking with mouse: Using the CheckdItems Property is confused too.
2
6825
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 form every thing works fine: checking items by code
1
8347
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 FillListViewFromFile(string path) {
0
8403
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8737
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6174
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.