foreach (string imageFile in files)
{
try
{
System.Drawing.Image myImage = Image.FromFile(imageFile);
myImageList.Images.Add(myImage);
myImage.Dispose();
}
catch { }
}
for (int i = 0; i < myImageList.Images.Count; i++) //I have a list view containing images
{
ListViewItem item = new ListViewItem();
item.ImageIndex = i;
imageListView.Items.Add(item);
}
private void imageListView_Clicked(object sender, EventArgs e)
{
string myImage = imageListView.SelectedItems.ToString(); //get the name of image selected
}
现在我想在选择图像时得到图像的名称,类似于imageListView_Clicked()的上述功能:
但它没有用。
答案 0 :(得分:0)
您应修改此部分代码,以将图像名称放入其Tag
属性:
foreach (string imageFile in files)
{
try
{
System.Drawing.Image myImage = Image.FromFile(imageFile);
myImage.Tag="image name for each item";//you can put for example image file name
myImageList.Images.Add(myImage);
myImage.Dispose();
}
catch { }
}
然后,当您想要获取所选图像的名称时,您可以使用以下代码:
private void imageListView_Clicked(object sender, EventArgs e)
{
String imgName= (String)imageListView.SelectedItems[0].ImageList.Images[0].Tag;
}
答案 1 :(得分:0)
我认为这会奏效。
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (listView1.SelectedItems.Count == 1)
{
MessageBox.Show(listView1.SelectedItems[0].Text);
}
}