Drag and drop file to listview
Cool guide and exactly what I was looking for. Thanks so much!
ListViewItem lstviewItem;
listView1.AllowDrop = true; listView1.View = View.Details; listView1.AllowDrop = true; listView1.Columns.Add("File"); listView1.Columns[0].Width = 500; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
DragEnter
event of the ListView.
private void listView1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true) { e.Effect = DragDropEffects.All; } }
DragDrop
event of the ListView.
private void listView1_DragDrop(object sender, DragEventArgs e) { try { string[] file; file = (string[])e.Data.GetData(DataFormats.FileDrop); foreach(string files in file) { listView1.Items.Add(lstviewItem); } } catch(Exception ex) { MessageBox.Show("error : " + ex.Message); } }
if (listView1.SelectedItems.Count == 0) return; pictureBox1.Image = Image.FromFile(listView1.SelectedItems[0].Text); this.Text = listView1.SelectedItems[0].Text;