none
C# DragDrop from listview on one form to picture box on another form

    Question

  • I'm working on a C# .NET WinForms app with a ListView and a PictureBox. I'm trying to get the code down for dragging an item from the listview to the picturebox and thereby placing an image in the picturebox. First, a few facts...

    1. Each ListViewItem's ToolTipText contains the image's full filepath. The path is formatted correctly and the image does exist at that location.
    2. All controls on my main form, including the main form, have AllowDrop property set to True.
    3. The ListView is in a tab of a tab control on a separate form. I need to drag a ListViewItem from that form to a picturebox on my main form.

    All that said, here's my code so far...

            private void lvImages_ItemDrag(object sender, ItemDragEventArgs e)
            {
                lvImages.DoDragDrop(lvImages.SelectedItems, DragDropEffects.Copy);
            }
    
            private void pictureBox1_DragEnter(object sender, DragEventArgs e)
            {
                e.Effect = DragDropEffects.Copy;
            }
    
            private void pictureBox1_DragDrop(object sender, DragEventArgs e)
            {
                try
                {
                    ListViewItem item = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
                    MessageBox.Show("Image Path: " + item.ToolTipText.ToString(), "Live, from frmStandardProject.pbQuestionImage_DragDrop...",
                                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (item.ToolTipText == null)
                        MessageBox.Show("File is missing!", "oops.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else
                    {
                        string strExt = Path.GetExtension(item.ToolTipText).ToLower();
                        if (strExt == ".jpg" || strExt == ".gif" || strExt == ".png" || strExt == ".bmp")
                        {
                            int index = item.ImageIndex;
    
                            Image image = Image.FromFile(item.ToolTipText);
    
                            if (image.Height > image.Width)
                                pictureBox1.Size = pictureBox1.MaximumSize = glbszPortraitSize;
    
                            else if (image.Width > image.Height)
                                pictureBox1.Size = pictureBox1.MaximumSize = glbszLandscapeSize;
    
                            pictureBox1.Image = image;
                            pictureBox1.Refresh();
                            btnClearpictureBox1Image.Enabled = true;
                        }
                    }
                }
                catch (Exception)
                {
                    if (btnClearpictureBox1Image.Enabled == true)
                        btnClearpictureBox1Image.Enabled = true;
                    else
                        btnClearpictureBox1Image.Enabled = false;
                }
            }
    With the above, code, nothing happens. The mouse cursor changes to the drag cursor when dragging outside the ListView, but when I release the mouse over the picturebox on the main form, the image does not appear in the picturebox.
    • Moved by CoolDadTxMVP Tuesday, February 06, 2018 3:28 PM Winforms related
    Monday, February 05, 2018 11:13 PM

Answers

  • Hello WikiGrrrl,

    For the latest version of IDE, it doesn't support drag and drop features for some control. I tested the label, textbox, panel, etc in VS 2017 and all seems that doesn't work. This is Microsoft reply.

    As for a workaround, I used richtextbox to instead picturebox.

      private void Form7_Load(object sender, EventArgs e)
            {
                richTextBox1.AllowDrop = true;
                richTextBox1.DragDrop += RichTextBox1_DragDrop;
            }
    
            private void RichTextBox1_DragDrop(object sender, DragEventArgs e)
            {
                var item=(ListView.SelectedListViewItemCollection)e.Data.GetData(typeof(ListView.SelectedListViewItemCollection));
    
                //set image to richtextbox
                Image img = Image.FromFile(item[0].ToolTipText);
                Clipboard.SetImage(img);
                richTextBox1.Paste();
            }

    Result.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 06, 2018 10:53 AM
  • Hello,

     For DragDrop to work, the object sender holds required data for transfer. Then

    the object target thru an event is sent the data that describes the transfer.

     

     Reference: https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/walkthrough-performing-a-drag-and-drop-operation-in-windows-forms

     

     Hope this helps :)

    • Marked as answer by WikiGrrrl Thursday, February 08, 2018 5:16 PM
    Tuesday, February 06, 2018 9:45 AM

All replies

  • Hello,

     For DragDrop to work, the object sender holds required data for transfer. Then

    the object target thru an event is sent the data that describes the transfer.

     

     Reference: https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/walkthrough-performing-a-drag-and-drop-operation-in-windows-forms

     

     Hope this helps :)

    • Marked as answer by WikiGrrrl Thursday, February 08, 2018 5:16 PM
    Tuesday, February 06, 2018 9:45 AM
  • Hello WikiGrrrl,

    For the latest version of IDE, it doesn't support drag and drop features for some control. I tested the label, textbox, panel, etc in VS 2017 and all seems that doesn't work. This is Microsoft reply.

    As for a workaround, I used richtextbox to instead picturebox.

      private void Form7_Load(object sender, EventArgs e)
            {
                richTextBox1.AllowDrop = true;
                richTextBox1.DragDrop += RichTextBox1_DragDrop;
            }
    
            private void RichTextBox1_DragDrop(object sender, DragEventArgs e)
            {
                var item=(ListView.SelectedListViewItemCollection)e.Data.GetData(typeof(ListView.SelectedListViewItemCollection));
    
                //set image to richtextbox
                Image img = Image.FromFile(item[0].ToolTipText);
                Clipboard.SetImage(img);
                richTextBox1.Paste();
            }

    Result.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 06, 2018 10:53 AM
  • Thanks for your responses! Sorry it took a while for me to finally respond. I'm marking this as answered.
    Thursday, February 08, 2018 5:18 PM