Skip to content

Drag from listbox and drop in picturebox C#

CherRon McLemore

Asked Jul 8, 2010, 5:35 AM EDT3 answers
Share
LinkedInX
I am working on a GUI where I have a listbox and a picturebox. The items in the listbox are image file paths. What I want to do is drag the file path from the listbox into the picturebox. When I run the code, nothing happens. Can someone help? Here is the code:

private void listbox1_MouseDown()
{
   string imagePath = (string) this.ListBox1.SelectedItem;
}

private void picBox1_MouseUp()
{
   if(e.Button == MouseButtons.Left)
      {
            picBox1.Load(imagePath);
      }
  imagePath = "";
}

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Hirendra Sisodiya
    0
    Hello CherRon
    use this code:
    private void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
    listBox1.DoDragDrop(listBox1.SelectedItem, DragDropEffects.Copy)
    }
     
    and
    private void picBox1_DragEnter(object sender, DragEventArgs e)
    {
    if (e.Data.GetDataPresent(DataFormats.Text))
    e.Effect = DragDropEffects.Copy;
    else
    e.Effect = DragDropEffects.None;
    }
    private void picBox1_DragDrop(object sender, DragEventArgs e)
    {
    picBox1.Load(e.Data.GetData(DataFormats.Text).ToString());
    }
    thanks
     
    Please mark as answer if it helps
    0
  • 0
    This code does not work. When I try to drag from the list box I get nothing.
    0
  • Hirendra Sisodiya
    0
    Hello Cherron
    Add this line on form load event
    this.pictureBox1.AllowDrop = true;
    check sample application for drag and drop with picturebox.
     
     
    thanks
    Please mark as answer if it helps
    0