Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

i am using listview to display images from folder,
now i have to drag selected image from listview drop it to the picturebox.


but when i am using Dodragdrop()
it does not allow m to drag the image from listview.

C#
private void listView1_MouseDown(object sender, MouseEventArgs e)
       {
           lv1_mdown = true;
           if (listView1.SelectedItems.Count == 0)
               return;
           else if (e.Button == MouseButtons.Right) return;
           else
           {
               string val = listView1.SelectedItems[0].Text;
               if (val == "" || val == " " || val == null)
                   return;
               listView1.DoDragDrop(val, DragDropEffects.Copy | DragDropEffects.Move);
           }
       }

       private void listView1_DragDrop(object sender, DragEventArgs e)
       {
           string textBox1 = e.Data.GetData(DataFormats.Text).ToString();
           string[] items = textBox1.Split(',');
           listView1.Items.Add(new ListViewItem(items, 0));
           lv1_mdown = false;
       }
Posted
Updated 24-Jan-12 1:05am

1 solution

Remember before using drag and drop you need to set the control to
C#
listView1.AllowDrop = true;


C#
private void Form1_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        e.Effect = DragDropEffects.Copy;
    }
    else
    {
        e.Effect = DragDropEffects.None;
    }
}

private void Form1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
        foreach (string fileLoc in filePaths)
        {
            // Code to read the contents of the text file
            if (File.Exists(fileLoc))
            {
                using (TextReader tr = new StreamReader(fileLoc))
                {
                    MessageBox.Show(tr.ReadToEnd());
                }
            }

        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900