Hi everyone today we explore C# ListView Images Text.That is how to display both images and text in a ListView in C#. Cheers.
SECTION 1: Our tools
- Visual Studio 2013
SECTION 2 : FORM
Please design your form.
SECTION 3: Our Form1.cs Class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CS_ListView_Imagestext
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//LV PROPERTIES
listView1.View = View.Details;
//CONSTRUCT COLUMNS
listView1.Columns.Add("Player", 250);
}
//POUPLATE WITH IMGS AN DTEX
private void populate()
{
//IMGLIST TO HOLD IMGS
ImageList imgs = new ImageList();
imgs.ImageSize = new Size(50, 50);
//LOAD IMGS FROM FILE. SPECIFY YOUR PATH FOR IMAGES
String[] paths = { };
paths = Directory.GetFiles("D:/Imgs");
try
{
foreach(String path in paths)
{
imgs.Images.Add(Image.FromFile(path));
}
}catch(Exception e)
{
MessageBox.Show(e.Message);
}
//BIND IMGS TO LISTVIEW
listView1.SmallImageList = imgs;
listView1.Items.Add("Michael Carrick", 0);
listView1.Items.Add("Diego Costa", 1);
listView1.Items.Add("David De Gea", 2);
listView1.Items.Add("Eden Hazard", 3);
listView1.Items.Add("Anders Herera", 4);
listView1.Items.Add("Oscar", 5);
listView1.Items.Add("Aaron Ramsey", 6);
}
private void populateBtn_Click(object sender, EventArgs e)
{
populate();
}
private void clearBtn_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
}
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
String selected = listView1.SelectedItems[0].SubItems[0].Text;
MessageBox.Show(selected);
}
}
}
Best Regards.