View list Highlight selection

Pretty simple question, but I think it will be much more complicated than it sounds.

I would like to keep the selection of the listview element there when the focus leaves the list view, for now I set the hideselection property to false and that’s fine .. it really causes a VERY light gray selection to stay after the list has lost focus, so what is my question is how can I correctly show that this element is still selected so that the user finds out about it, something like changing the color of the text of the lines or the background color? or just keeping it selected, how does the first line turn blue when you select it?

I looked through intelisense and can't find anything for a color property or element or selected individual color element?

It should exist, although due to the fact that the selected elements have their own background color, where could I change this?

Oh, and viewing the list should remain in a detailed view, which means that I can’t use the only method that I managed to find during a google search

thanks

+1
c # listview visual-studio-2010
source share
2 answers

Here is a solution for ListView that does not allow multiple selections and does not have images (e.g. flags).

  • Set event handlers for ListView (in this example it is called listView1):
    • DrawItem
    • Leave (called when ListView focus is lost)
  • Declare a global variable int (i.e. a member of the form that contains the ListView, in this example it is called gListView1LostFocusItem) and set it to -1
    • int gListView1LostFocusItem = -1;
  • Implement event handlers as follows:

     private void listView1_Leave(object sender, EventArgs e) { // Set the global int variable (gListView1LostFocusItem) to // the index of the selected item that just lost focus gListView1LostFocusItem = listView1.FocusedItem.Index; } private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e) { // If this item is the selected item if (e.Item.Selected) { // If the selected item just lost the focus if (gListView1LostFocusItem == e.Item.Index) { // Set the colors to whatever you want (I would suggest // something less intense than the colors used for the // selected item when it has focus) e.Item.ForeColor = Color.Black; e.Item.BackColor = Color.LightBlue; // Indicate that this action does not need to be performed // again (until the next time the selected item loses focus) gListView1LostFocusItem = -1; } else if (listView1.Focused) // If the selected item has focus { // Set the colors to the normal colors for a selected item e.Item.ForeColor = SystemColors.HighlightText; e.Item.BackColor = SystemColors.Highlight; } } else { // Set the normal colors for items that are not selected e.Item.ForeColor = listView1.ForeColor; e.Item.BackColor = listView1.BackColor; } e.DrawBackground(); e.DrawText(); } 

Note. This solution may cause some flicker. The fix for this involves subclassing the ListView control so that you can change the protected DoubleBuffered property to true.

 public class ListViewEx : ListView { public ListViewEx() : base() { this.DoubleBuffered = true; } } 

I created a class library of the above class to add it to the toolbar.

+4
source share

A possible solution would be this answer to another question:

How do I change the color of my favorites list, even if you focus on another control?

0
source share

All Articles