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 = "";
}
Drag from listbox and drop in picturebox C#
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Hirendra SisodiyaRank #256 · 9.8K pointsPosted Jul 8, 2010, 6:33 AM EDT
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
CherRon McLemore24 pointsPosted Jul 9, 2010, 5:42 AM EDT
Hirendra SisodiyaRank #256 · 9.8K pointsPosted Jul 13, 2010, 9:45 PM EDT
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