I wish to make a list of items with pictures, the amount of items can vary from 1-60 and for each item I wish to also show data. I believe the best way of going about this is using the ListView in c#. is this true and if so how would I go about doing this? i have also thought about using interactive images within a scrolling window
Asked
Active
Viewed 3.7k times
3
-
have you done a google search on `Adding Images in Listview`? there are 1000's of examples on the internet to do this ..show more effort please – MethodMan Mar 12 '13 at 16:12
-
@DJKRAZE i am aware, i just wanted to make sure this was the best way to do so. – Froodle Mar 12 '13 at 16:13
-
look at the link in the above comment this is totally possible – MethodMan Mar 12 '13 at 16:14
1 Answers
10
If you want to do this in the designer, you can take the following steps to add the images to the ListView control:
- Switch to the designer, click on the ImageList component on the Component Tray, there will be a smart tag appear on the top-right corner of the ImageList.
- Click the smart tag, and click "Choose Images" on the pane.
- On the pop-up Image Collection Editor dialog, choose the images from the folder your want.
- Click OK to finish adding images to the ImageList.
- Click the ListView on the form, there will be a smart tag appear on the top-right corner.
- Click the smart tag, you will find there're three ComboBoxes there, choose a ImageList from the list as you want.
- Click the "Add items" option on the smart tag, a ListViewItem Collection Editor will appear, you can add items to the ListView, it's important here to set the ImageIndex or ImageKey property, or the image won't appear.
- Click OK to finish item editing, now you'll find the images are displayed on the ListView.
If you want to add the images to the ListView by code, you can do something like this`
Code Snippet
private void Form10_Load(object sender, EventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(@"c:\pic");
foreach (FileInfo file in dir.GetFiles())
{
try
{
this.imageList1.Images.Add(Image.FromFile(file.FullName));
}
catch{
Console.WriteLine("This is not an image file");
}
}
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(32, 32);
this.listView1.LargeImageList = this.imageList1;
//or
//this.listView1.View = View.SmallIcon;
//this.listView1.SmallImageList = this.imageList1;
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView1.Items.Add(item);
}
}

dotmido
- 1,374
- 3
- 13
- 29
-
The above answer is ok, but is there is no link between a filename and its image if you wanted to manipulate the files later on. so I would add a key of file.Name to the image added to the imageList. `imageList1.Images.Add(file.Name,Image.FromFile(file.FullName)); ... ListViewItem item = new ListViewItem(file.Name); item.SubItems.Add(file.Extension); item.ImageKey = file.Name;` – IEnumerable Apr 05 '14 at 00:30