copy-paste listview item using drag-drop

Answered copy-paste listview item using drag-drop

  • Tuesday, January 31, 2012 5:55 AM
     
     

    I have loaded some images in a listview using imagelist in c#. Now I want to copy any image and paste into another image using drag and drop. How could I do this? sample code would be helpful.

    Thanks in advance.

    • Moved by CoolDadTxMVP Tuesday, January 31, 2012 3:45 PM Winforms related (From:Visual C# General)
    •  

All Replies

  • Tuesday, January 31, 2012 6:16 AM
     
     

    Hi..

    Already discused in MSDN and may be it will helps to you..

    Try this.

    http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/09499630-32e3-479d-9d4d-9c1c844aa861

    http://bytes.com/topic/c-sharp/answers/720475-drag-drop-images-listview

    http://www.pcreview.co.uk/forums/drag-drop-images-listview-t3272552.html

    I hope it will helps to you..


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"
  • Tuesday, January 31, 2012 9:27 AM
     
     

    sorry, I couldn't figure it out.

    I want to copy any image and paste into another image within this listview using drag and drop. that is copy first image and paste into second image in the same listview.

  • Tuesday, January 31, 2012 2:03 PM
     
     

    A few questions:

    1. Are these images within the same form, application or both?
    2. Do you need an actual copy of the graphic or just the appearance of the image in the second ListView?
    3. Does this need to between just two ListViews (one to one relationship) or are there multiple ListViews in play?

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
  • Tuesday, January 31, 2012 2:54 PM
     
     

    ..

    I want to copy any image and paste into another image within this listview using drag and drop. that is copy first image and paste into second image in the same listview.

    Hi,

    what do you mean with paste into another image? Should the dragged image be drawn on top of the other image? Or should the image be replaced by the dragged one?

    Regards,

      Thorsten

  • Wednesday, February 01, 2012 3:43 AM
     
     

    1. both

    2. There is only one listview. I want to copy a listviewitem's image and replace into other listviewitem's image in the same listview.

  • Wednesday, February 01, 2012 3:44 AM
     
     
    it should the image be replaced by the dragged one.
  • Wednesday, February 01, 2012 9:00 AM
    Moderator
     
     

    I want to copy any image and paste into another image within this listview using drag and drop. that is copy first image and paste into second image in the same listview.


    They are seems almost the same thing to me, what is your question explicitly? The answer to your need maybe not too tough, but we are not able to understand your requirement explicitly. Maybe this article would give you some help http://www.codeproject.com/Articles/7034/Drag-and-Drop-List-View, it Drag and Drop list items within listviews or to other listviews without manual intervention. If this desn't help, please elaborate more clearly about the question.

    Sincerely,

    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
  • Wednesday, February 01, 2012 3:13 PM
     
     Proposed Answer Has Code

    Here is a fairly simple example.

     public partial class Form1 : Form
     {
      private bool m_DragInProgress;
    
      public Form1()
      {
       InitializeComponent();
      }
    
      private void listView1_MouseMove(object sender, MouseEventArgs e)
      {
       if (e.Button == MouseButtons.Left)
       {
        if (!m_DragInProgress && listView1.GetItemAt(e.Location.X, e.Location.Y) != null)
        {
         m_DragInProgress = true;
         listView1.DoDragDrop(listView1.GetItemAt(e.Location.X, e.Location.Y), DragDropEffects.Copy);
        }
       }
       else
       {
        m_DragInProgress = false;
       }
      }
    
      private void listView1_DragOver(object sender, DragEventArgs e)
      {
       if (m_DragInProgress)
       {
        Point l_point = listView1.PointToClient(new Point(e.X, e.Y));
        ListViewItem l_item = listView1.GetItemAt(l_point.X, l_point.Y);
    
        if (l_item != null && l_item != listView1.SelectedItems[0])
        {
         e.Effect = DragDropEffects.Copy;
        }
        else
        {
         e.Effect = DragDropEffects.None;
        }
       }
      }
    
      private void listView1_DragDrop(object sender, DragEventArgs e)
      {
       if (m_DragInProgress)
       {
        Point l_point = listView1.PointToClient(new Point(e.X, e.Y));
        ListViewItem l_item = listView1.GetItemAt(l_point.X, l_point.Y);
    
        if (l_item != null && l_item != listView1.SelectedItems[0])
        {
         l_item.ImageIndex = listView1.SelectedItems[0].ImageIndex;
         l_item.ImageKey = listView1.SelectedItems[0].ImageKey;
        }
    
        m_DragInProgress = false;
       }
      }
     }
    
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
  • Wednesday, February 01, 2012 3:38 PM
     
      Has Code

    Hi,

      private void listView1_DragDrop(object sender, DragEventArgs e)
      {
       if (m_DragInProgress)
       {
        Point l_point = listView1.PointToClient(new Point(e.X, e.Y));
        ListViewItem l_item = listView1.GetItemAt(l_point.X, l_point.Y);
    
        if (l_item != null && l_item != listView1.SelectedItems[0])
        {
         l_item.ImageIndex = listView1.SelectedItems[0].ImageIndex;
         l_item.ImageKey = listView1.SelectedItems[0].ImageKey;
        }
    
        m_DragInProgress = false;
       }
    
    

    Note that you only should set the ImageKey, when also using the ImageKey in your listview. If you assign images only by the index, comment out that line.

    Regards,

      Thorsten

  • Wednesday, February 01, 2012 3:43 PM
     
     
    Absolutely correct.  When I was building my example, I was trying to catch both instances, but one does cancel the other out.  You should only keep whichever Image assignment mechanism that you are using.  I was using ImageKey, so it had no effect.
    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
  • Wednesday, February 01, 2012 3:46 PM
     
     Answered Has Code

    Corrected:

     public partial class Form1 : Form
     {
      private bool m_DragInProgress;
    
      public Form1()
      {
       InitializeComponent();
      }
    
      private void listView1_MouseMove(object sender, MouseEventArgs e)
      {
       if (e.Button == MouseButtons.Left)
       {
        if (!m_DragInProgress && listView1.GetItemAt(e.Location.X, e.Location.Y) != null)
        {
         m_DragInProgress = true;
         listView1.DoDragDrop(listView1.GetItemAt(e.Location.X, e.Location.Y), DragDropEffects.Copy);
        }
       }
       else
       {
        m_DragInProgress = false;
       }
      }
    
      private void listView1_DragOver(object sender, DragEventArgs e)
      {
       if (m_DragInProgress)
       {
        Point l_point = listView1.PointToClient(new Point(e.X, e.Y));
        ListViewItem l_item = listView1.GetItemAt(l_point.X, l_point.Y);
    
        if (l_item != null && l_item != listView1.SelectedItems[0])
        {
         e.Effect = DragDropEffects.Copy;
        }
        else
        {
         e.Effect = DragDropEffects.None;
        }
       }
      }
    
      private void listView1_DragDrop(object sender, DragEventArgs e)
      {
       if (m_DragInProgress)
       {
        Point l_point = listView1.PointToClient(new Point(e.X, e.Y));
        ListViewItem l_item = listView1.GetItemAt(l_point.X, l_point.Y);
    
        if (l_item != null && l_item != listView1.SelectedItems[0])
        {
         //l_item.ImageIndex = listView1.SelectedItems[0].ImageIndex;
         // OR
         l_item.ImageKey = listView1.SelectedItems[0].ImageKey;
        }
    
        m_DragInProgress = false;
       }
      }
     }
    
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.
  • Wednesday, February 01, 2012 3:52 PM
     
     Answered Has Code

    Or, better yet, handle both:

     public partial class Form1 : Form
     {
      private bool m_DragInProgress;
    
      public Form1()
      {
       InitializeComponent();
      }
    
      private void listView1_MouseMove(object sender, MouseEventArgs e)
      {
       if (e.Button == MouseButtons.Left)
       {
        if (!m_DragInProgress && listView1.GetItemAt(e.Location.X, e.Location.Y) != null)
        {
         m_DragInProgress = true;
         listView1.DoDragDrop(listView1.GetItemAt(e.Location.X, e.Location.Y), DragDropEffects.Copy);
        }
       }
       else
       {
        m_DragInProgress = false;
       }
      }
    
      private void listView1_DragOver(object sender, DragEventArgs e)
      {
       if (m_DragInProgress)
       {
        Point l_point = listView1.PointToClient(new Point(e.X, e.Y));
        ListViewItem l_item = listView1.GetItemAt(l_point.X, l_point.Y);
    
        if (l_item != null && l_item != listView1.SelectedItems[0])
        {
         e.Effect = DragDropEffects.Copy;
        }
        else
        {
         e.Effect = DragDropEffects.None;
        }
       }
      }
    
      private void listView1_DragDrop(object sender, DragEventArgs e)
      {
       if (m_DragInProgress)
       {
        Point l_point = listView1.PointToClient(new Point(e.X, e.Y));
        ListViewItem l_item = listView1.GetItemAt(l_point.X, l_point.Y);
    
        if (l_item != null && l_item != listView1.SelectedItems[0])
        {
         if (listView1.SelectedItems[0].ImageIndex != -1)
         {
          l_item.ImageIndex = listView1.SelectedItems[0].ImageIndex;
         }
         else
         {
          l_item.ImageKey = listView1.SelectedItems[0].ImageKey;
         }
        }
    
        m_DragInProgress = false;
       }
      }
     }
    
    

     


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.