0

This questions has nothing in common with Reorder / move / dragdrop ListViewItems within the same ListView Control in C# Windows Forms because it will only work for large/small icons view mode!

I found some nice little code here:

http://snipplr.com/view/33427/

using System.Drawing;
using System.Windows.Forms;

namespace System.Windows.Forms // May need to set to something else
{
    /// <summary>
    /// A ListView with DragDrop reordering.
    /// <see cref="http://support.microsoft.com/kb/822483/en-us"/>
    /// </summary>
    public class ListViewWithReordering : ListView
    {
        protected override void OnItemDrag(ItemDragEventArgs e)
        {
            base.OnItemDrag(e);
            //Begins a drag-and-drop operation in the ListView control.
            this.DoDragDrop(this.SelectedItems, DragDropEffects.Move);
        }

        protected override void OnDragEnter(DragEventArgs drgevent)
        {
            base.OnDragEnter(drgevent);
            int len = drgevent.Data.GetFormats().Length - 1;
            int i;
            for (i = 0; i <= len; i++)
            {
                if (drgevent.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection"))
                {
                    //The data from the drag source is moved to the target. 
                    drgevent.Effect = DragDropEffects.Move;
                }
            }

        }

        protected override void OnDragDrop(DragEventArgs drgevent)
        {
            base.OnDragDrop(drgevent);
            //Return if the items are not selected in the ListView control.
            if (this.SelectedItems.Count == 0)
            {
                return;
            }
            //Returns the location of the mouse pointer in the ListView control.
            Point cp = this.PointToClient(new Point(drgevent.X, drgevent.Y));
            //Obtain the item that is located at the specified location of the mouse pointer.
            ListViewItem dragToItem = this.GetItemAt(cp.X, cp.Y);
            if (dragToItem == null)
            {
                return;
            }
            //Obtain the index of the item at the mouse pointer.
            int dragIndex = dragToItem.Index;
            ListViewItem[] sel = new ListViewItem[this.SelectedItems.Count];
            for (int i = 0; i <= this.SelectedItems.Count - 1; i++)
            {
                sel[i] = this.SelectedItems[i];
            }
            for (int i = 0; i < sel.GetLength(0); i++)
            {
                //Obtain the ListViewItem to be dragged to the target location.
                ListViewItem dragItem = sel[i];
                int itemIndex = dragIndex;
                if (itemIndex == dragItem.Index)
                {
                    return;
                }
                if (dragItem.Index < itemIndex)
                    itemIndex++;
                else
                    itemIndex = dragIndex + i;
                //Insert the item at the mouse pointer.
                ListViewItem insertItem = (ListViewItem)dragItem.Clone();
                this.Items.Insert(itemIndex, insertItem);
                //Removes the item from the initial location while 
                //the item is moved to the new location.
                this.Items.Remove(dragItem);
            }
        }
    }
}

which is based on / was adapted from http://www.codeproject.com/Articles/4576/Drag-and-Drop-ListView-row-reordering.

Unfortunately the code download doesn't work.

However - how is this code beeing used? Simply pasting it in my forms-class won't work. I supposed this code simply overrides all drag events from all listviews within a form?!

Sorry this is such a stupid question, but how do I use this code?

2
  • 1
    You realize it is a class on its own, right? It needs to go in it's own file
    – rene
    Feb 4, 2016 at 10:40
  • Sure. That's what I tried. But where / how is the invocation? Feb 4, 2016 at 10:44

3 Answers 3

4

Someone has kindly taken the standard .NET ListView control and used something called Inheritance to make their own version with extra functionality. You should read up on it, it's a useful thing to know.

To get it to work in your project you need to add this class as described below.

I assume you're using Visual Studio, you haven't mentioned otherwise.

  1. In Solution Explorer on the right hand side Right Click on your solution and go to Add > Class.

  2. Name the class whatever you like (ListViewWithReordering.cs would make the most sense) and then open that file. Past all of this code into the file.

  3. Where it says namespace at the top you'll want to edit this to match your own programs namespace.

  4. Build your project.

  5. In the Toolbox on the left where you normally see all of the standard .NET controls there should be a section at the top that now contains ListViewWithReordering.

  6. Drag this control onto your project.

  7. Done! Use it like any other ListView control. The rest should work automatically (as long as this class is indeed reliable).

5
  • Thank you Equlsk. I know what inheritance is, but I didn't know, that you can override standard win-forms in such a easy way o.O Feb 4, 2016 at 10:54
  • And I didn't expect it to appear under "LoginView-Components" :) Feb 4, 2016 at 10:54
  • No problem. It's one of those "it's easy when you know how" problems. Have fun!
    – Equalsk
    Feb 4, 2016 at 10:55
  • A few mods are needed to make this control work. AutoArrange needs to be set to false to allow drag and drop within ListViewWithReordering control, and the control needs to be refreshed from outside the Drop event to show the re-ordered images. I do this with a timer and change the control View mode to Tile and then back to whichever of LargeIcon and SmallIcon was used. Control and ImageLists need explicit Dispose after form containing it is closed to avoid Out of Memory problem on reentry. Oct 13, 2019 at 12:07
  • +1 I knew about overriding standard winforms pretty well, but apparently not as well as I thought.. Learn something new everyday, right? Thanks :)
    – Momoro
    May 18, 2020 at 4:09
1

I created a design example of drag and drop, download the project on the following link. Any questions ask again.

https://drive.google.com/file/d/0B21l6Fz0byBMSi1VTm52V2E4VVE/view?usp=sharing&resourcekey=0-jNA3sA85A6lygH3TyISstA

1
  • Thanks for your code. Unfortunately is the code from my initial post not working properly... And it's from Microsoft. Feb 4, 2016 at 13:42
-1

First add this class to your project.

Then, If you already have a listview from the forms designer, go to the InitializeComponent() method of your form constructor. There your listview is declared as:

private System.Windows.Forms.ListView listView1;

Change this declaration to:

private ListViewWithReordering listView1;
1
  • 3
    don't touch the generated file.
    – rene
    Feb 4, 2016 at 10:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.