how i'm adding the files to the listview in constructor
listView1.View = View.List;
string[] files = Directory.GetFiles(@"d:\New folder (4)");
for (int i = 0; i < files.Length; i )
{
listView1.Items.Add(files[i]);
}
for example the first file in the listview is d:\New folder (4)\myimage.jpg when running the application i want to display the file in picturebox1
i tried this in the constructor
img1 = Image.FromFile(listView1.Items[0].ToString());
but getting error on that line
System.NotSupportedException: 'The given path's format is not supported.'
CodePudding user response:
You must read the text of an item. Items[0] returns an object. You must read the Items[0].Text: so edit this line:
//img1 = Image.FromFile(listView1.Items[0].ToString());
img1 = Image.FromFile(listView1.Items[0].Text);
CodePudding user response:
You need to react to the ItemSelectionChange event of the ListView control.
Also use the Text property of the ListViewItem to access the text of the item.
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected && e.Item != null)
{
MessageBox.Show(e.Item.Text);
}
}

