Reputation: 364
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!
Upvotes: 3
Views: 12457
Reputation: 326
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.
Upvotes: 3
Reputation: 3297
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.
Upvotes: 11