1
           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() 函数:

但它不起作用。

4

2 回答 2

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;

 }
于 2013-06-24T10:00:37.980 回答
0

我认为这会奏效。

    private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        if (listView1.SelectedItems.Count == 1)
        {

           MessageBox.Show(listView1.SelectedItems[0].Text);
        }
    }
于 2014-09-16T10:56:12.923 回答