You can get what you are after by relying on the method associated with the SelectedIndexChanged Event
. Sample code:
private void myLV_SelectedIndexChanged(object sender, EventArgs e)
{
if (myLV.SelectedItems.Count > 0)
{
foreach (ListViewItem item in myLV.SelectedItems)
{
if (item.ForeColor == Color.Gray)
{
item.Selected = false;
}
else
{
ListViewItem tempItem = item;
grayOut2(ref tempItem);
}
}
}
}
This code grays out any item you select if it hasn't been selected before (not grayed out); otherwise, it avoids it to be selected. GrayOut2
is a version of your function only taking into consideration the given item.
internal static void grayOut2(ref ListViewItem item)
{
//change each selected item to gray text
//currently, multiselect is turned off, so this will only be one item at a time
item.UseItemStyleForSubItems = false;
item.ForeColor = Color.Gray;
item.Font = new Font("MS Sans Serif", 10, FontStyle.Italic);
item.Selected = false;
}