简体   繁体   中英

How do I add Icon in listview columns according to the items in c# windows form application?

I have a listview and columns, I need to add icons in listview like, If I select the row means it should be open mail icon, if i didn't selected row means its as it is Mail icon.

Can you guys tell me the logic....

Thanks!

Since I don't know how you add the items to your ListView , I can only provide you an example snippet. Add an ImageList to your solution and add those two icons (read and unread mail icon) to this list. To add an item to your list view control using an image you could do the following:

ListViewItem item = new ListViewItem();
item.SubItems.Add("item1");
item.SubItems.Add("item2");

item.ImageIndex = 0;
listView1.Items.Add(item);

To change the icon when selecting an icon you should use SelectedIndexChanged event:

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    listView1.SelectedItems[0].ImageIndex = 1;
}

Remember to set the SmallImageList property. You can do this in designer using the list view properties or programmatically:

listView1.SmallImageList = imageList1;

Note that you have to set the ImageIndex property to the index which your icon is in your ImageList , otherwise it will not show any icon.

You can use state image list:

var listViewItem1 = new ListViewItem(new string[] { "", "Subject2"}, -1);
var imageList1 = new ImageList(this.components);
var listView1 = new ListView();
...
// setup listview
listView1.StateImageList = imageList1;
...
// set up initial image index
listViewItem1.StateImageIndex = 1;

Then if you click on row image index is changed to next one. So if in your image list you have mail and mail opened images it will be switched between them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM