I have setup the imageList and the listView. Ideally I would like the filename to show under the image.
UPDATED using the item.Tag mentioned below I was able to move some things around. I now have the file name tracking which file is selected. Now I just have to see if I can get the file name to show up in the listview.
Here's the new code:
private void GetImages()
{
DirectoryInfo dir = new DirectoryInfo(@"c:\pics");
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(67, 100);
this.listView1.LargeImageList = this.imageList1;
int j = 0;
foreach (FileInfo file in dir.GetFiles())
{
try
{
imageList1.Images.Add(file.Name, Image.FromFile(file.FullName));
ListViewItem item = new ListViewItem(file.Name);
item.Tag = file.Name;
item.ImageIndex = j;
this.listView1.Items.Add(item);
j++;
}
catch
{
Console.WriteLine("This is not an image file");
}
}
}
private void listView1_Click(object sender, EventArgs e)
{
ListViewItem item = listView1.SelectedItems[0];
label1.Text = item.Tag.ToString();
int itemIndex = listView1.SelectedIndices[0];
Image img = imageList1.Images[itemIndex];
pictureBox1.Image = img;
}
Ok I think I've simplified some of the other answers. With this way I'm able to get the filename to show up under the image. Also when the image is selected I'm able harness it.
I'm using the item.tag to hold the file name.
private void GetImages()
{
DirectoryInfo dir = new DirectoryInfo(@"c:\pics");
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(100, 100);
this.listView1.LargeImageList = this.imageList1;
int j = 0;
foreach (FileInfo file in dir.GetFiles())
{
try
{
//this.imageList1.Images.Add(Image.FromFile(file.FullName));
imageList1.Images.Add(file.Name, Image.FromFile(file.FullName));
ListViewItem item = new ListViewItem(file.Name);
item.Tag = file.Name;
item.ImageIndex = j;
this.listView1.Items.Add(item);
j++;
}
catch
{
Console.WriteLine("This is not an image file");
}
}
}
Now in either the listview1_Click or some other call to action I am able to use the follow to harness the filename tag.
ListViewItem item = listView1.SelectedItems[0];
txtNewWM.Text = item.Tag.ToString();