Reorder / move / dragdrop ListViewItems within the same ListView Control in C# Windows Forms

前端 未结 3 950
清歌不尽
清歌不尽 2021-01-15 04:53

I have a ListView in LargeIcon View in C# 2008 System Windows Forms. Now I would like to move an item of this ListView within the same ListView on another p

相关标签:
3条回答
  • 2021-01-15 05:34

    we can use the following code to get item ordered by position

        SortedDictionary<Tuple<int, int>, string> points = new SortedDictionary<Tuple<int, int>, string>();
        string debug1 = "", debug2 = "";
        foreach (ListViewItem item in listView1.Items)
        {
            Tuple<int, int> tp = new Tuple<int,int>(item.Position.Y, item.Position.X);
            points.Add(tp, item.Text);
            debug1 += item.Text;
        }
    
        foreach (KeyValuePair<Tuple<int, int>, string> kvp in points)
        {
            debug2 += kvp.Value;
        }
        MessageBox.Show(debug1); //orignal order
        MessageBox.Show(debug2); //sort by position
    
    0 讨论(0)
  • 2021-01-15 05:35

    There is working example of drag and drop re-ordering in this article or same here. Provided code also supports Insertion Mark. Below the code from the article:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class ListViewInsertionMarkExample : Form
    {
        private ListView myListView; 
    
        public ListViewInsertionMarkExample()
        {
            // Initialize myListView.
            myListView = new ListView();
            myListView.Dock = DockStyle.Fill;
            myListView.View = View.LargeIcon;
            myListView.MultiSelect = false;
            myListView.ListViewItemSorter = new ListViewIndexComparer();
    
            // Initialize the insertion mark.
            myListView.InsertionMark.Color = Color.Green;
    
            // Add items to myListView.
            myListView.Items.Add("zero");
            myListView.Items.Add("one");
            myListView.Items.Add("two");
            myListView.Items.Add("three");
            myListView.Items.Add("four");
            myListView.Items.Add("five");
            
            // Initialize the drag-and-drop operation when running
            // under Windows XP or a later operating system.
            if (OSFeature.Feature.IsPresent(OSFeature.Themes))
            {
                myListView.AllowDrop = true;
                myListView.ItemDrag += new ItemDragEventHandler(myListView_ItemDrag);
                myListView.DragEnter += new DragEventHandler(myListView_DragEnter);
                myListView.DragOver += new DragEventHandler(myListView_DragOver);
                myListView.DragLeave += new EventHandler(myListView_DragLeave);
                myListView.DragDrop += new DragEventHandler(myListView_DragDrop);
            }
    
            // Initialize the form.
            this.Text = "ListView Insertion Mark Example";
            this.Controls.Add(myListView);
        }
    
        [STAThread]
        static void Main() 
        {
            Application.EnableVisualStyles();
            Application.Run(new ListViewInsertionMarkExample());
        }
    
        // Starts the drag-and-drop operation when an item is dragged.
        private void myListView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            myListView.DoDragDrop(e.Item, DragDropEffects.Move);
        }
    
        // Sets the target drop effect.
        private void myListView_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }
    
        // Moves the insertion mark as the item is dragged.
        private void myListView_DragOver(object sender, DragEventArgs e)
        {
            // Retrieve the client coordinates of the mouse pointer.
            Point targetPoint = 
                myListView.PointToClient(new Point(e.X, e.Y));
    
            // Retrieve the index of the item closest to the mouse pointer.
            int targetIndex = myListView.InsertionMark.NearestIndex(targetPoint);
    
            // Confirm that the mouse pointer is not over the dragged item.
            if (targetIndex > -1) 
            {
                // Determine whether the mouse pointer is to the left or
                // the right of the midpoint of the closest item and set
                // the InsertionMark.AppearsAfterItem property accordingly.
                Rectangle itemBounds = myListView.GetItemRect(targetIndex);
                if ( targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) )
                {
                    myListView.InsertionMark.AppearsAfterItem = true;
                }
                else
                {
                    myListView.InsertionMark.AppearsAfterItem = false;
                }
            }
    
            // Set the location of the insertion mark. If the mouse is
            // over the dragged item, the targetIndex value is -1 and
            // the insertion mark disappears.
            myListView.InsertionMark.Index = targetIndex;
        }
    
        // Removes the insertion mark when the mouse leaves the control.
        private void myListView_DragLeave(object sender, EventArgs e)
        {
            myListView.InsertionMark.Index = -1;
        }
    
        // Moves the item to the location of the insertion mark.
        private void myListView_DragDrop(object sender, DragEventArgs e)
        {
            // Retrieve the index of the insertion mark;
            int targetIndex = myListView.InsertionMark.Index;
    
            // If the insertion mark is not visible, exit the method.
            if (targetIndex == -1) 
            {
                return;
            }
    
            // If the insertion mark is to the right of the item with
            // the corresponding index, increment the target index.
            if (myListView.InsertionMark.AppearsAfterItem) 
            {
                targetIndex++;
            }
    
            // Retrieve the dragged item.
            ListViewItem draggedItem = 
                (ListViewItem)e.Data.GetData(typeof(ListViewItem));
    
            // Insert a copy of the dragged item at the target index.
            // A copy must be inserted before the original item is removed
            // to preserve item index values. 
            myListView.Items.Insert(
                targetIndex, (ListViewItem)draggedItem.Clone());
    
            // Remove the original copy of the dragged item.
            myListView.Items.Remove(draggedItem);
        }
    
        // Sorts ListViewItem objects by index.
        private class ListViewIndexComparer : System.Collections.IComparer
        {
            public int Compare(object x, object y)
            {
                return ((ListViewItem)x).Index - ((ListViewItem)y).Index;
            }
        }
    }
    

    Beware of where you add myListView.ListViewItemSorter = new ListViewIndexComparer();, I had performance issues with large list. To solve it add Comparer after ListView items are added.

    0 讨论(0)
  • 2021-01-15 05:37

    In fact the feature you talk about is not supported by Winforms not C#. C# has nothing to do with such a feature; it's a UI technology feature not a language feature. However, to solve this, we have little code here. It supports the Position property for each ListViewItem to use for that purpose (in LargeIcon view). Another important property is AutoArrange, this should be set to false to allow the Position to take effect. Here is the code:

    ListViewItem heldDownItem;
    Point heldDownPoint;
    //MouseDown event handler for your listView1
    private void listView1_MouseDown(object sender, MouseEventArgs e)
    {            
        //listView1.AutoArrange = false;
        heldDownItem = listView1.GetItemAt(e.X,e.Y);
        if (heldDownItem != null) {
          heldDownPoint = new Point(e.X - heldDownItem.Position.X, 
                                    e.Y - heldDownItem.Position.Y);
        }
    }
    //MouseMove event handler for your listView1
    private void listView1_MouseMove(object sender, MouseEventArgs e)
    {
        if (heldDownItem != null){
            heldDownItem.Position = new Point(e.Location.X - heldDownPoint.X, 
                                              e.Location.Y - heldDownPoint.Y);
        }
    }
    //MouseUp event handler for your listView1
    private void listView1_MouseUp(object sender, MouseEventArgs e)
    {
        heldDownItem = null;
        //listView1.AutoArrange = true;         
    }
    

    NOTE: as you can see, I let 2 commented code lines listView1.AutoArrange there, if you want to reorder instead of changing the ListViewItem position you can uncomment those lines. I can notice some flicker here (this is normal when you deal with a winforms ListView), so you should use this code (can be placed in the form constructor) to enable DoubleBuffered:

    typeof(Control).GetProperty("DoubleBuffered", 
                                 System.Reflection.BindingFlags.NonPublic |
                                 System.Reflection.BindingFlags.Instance)
                   .SetValue(listView1, true, null);
    
    0 讨论(0)
提交回复
热议问题