81

I'm trying to select the first item in a ListView programmatically, but it doesn't appear to have been selected. I am using the following code:

if (listView1.Items.Count > 0)
    listView1.Items[0].Selected = true;

Actually I've had this problem before but I can't remember how I managed to solve it!

Homam
  • 23,263
  • 32
  • 111
  • 187

7 Answers7

95

Most likely, the item is being selected, you just can't tell because a different control has the focus. There are a couple of different ways that you can solve this, depending on the design of your application.

  1. The simple solution is to set the focus to the ListView first whenever your form is displayed. The user typically sets focus to controls by clicking on them. However, you can also specify which controls gets the focus programmatically. One way of doing this is by setting the tab index of the control to 0 (the lowest value indicates the control that will have the initial focus). A second possibility is to use the following line of code in your form's Load event, or immediately after you set the Selected property:

    myListView.Select();
    

    The problem with this solution is that the selected item will no longer appear highlighted when the user sets focus to a different control on your form (such as a textbox or a button).

  2. To fix that, you will need to set the HideSelection property of the ListView control to False. That will cause the selected item to remain highlighted, even when the control loses the focus.

    When the control has the focus, the selected item's background will be painted with the system highlight color. When the control does not have the focus, the selected item's background will be painted in the system color used for grayed (or disabled) text.

    You can set this property either at design time, or through code:

    myListView.HideSelection = false;
    
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
51
if (listView1.Items.Count > 0)
{
    listView1.Items[0].Selected = true;
    listView1.Select();
}

list items do not appear selected unless the control has the focus (or you set the HideSelection property to false)

Daniël van den Berg
  • 2,197
  • 1
  • 20
  • 45
VikciaR
  • 3,324
  • 20
  • 32
  • 4
    Code snippets without explanations don't count as answers. – Cody Gray - on strike Apr 26 '11 at 13:53
  • 8
    But it answered question and it is self explanatory :-) – VikciaR Apr 26 '11 at 13:54
  • 12
    No, it isn't self-explanatory. The only way it makes sense is if you understand that list items do not appear selected unless the control has the focus (or you set the `HideSelection` property to false). And if you knew that, you wouldn't have to ask this question in the first place. More importantly, this breaks when the user clicks somewhere else on the form. Suddenly the selected item no longer appears to be selected! The asker who just copies and pastes code from your answer won't know what's happened, and they'll be back asking another question. – Cody Gray - on strike Apr 26 '11 at 13:57
  • 2
    Ok, agree - your answer is more comprehensive and fits question better. – VikciaR Apr 27 '11 at 06:20
  • If the element is not visible you have to add `UsersLst.Items[index].EnsureVisible();` in order to show the element. – Ruben Giaquinto Aug 29 '17 at 14:49
15

I know this is an old question, but I think this is the definitive answer.

listViewRamos.Items[i].Focused = true;
listViewRamos.Items[i].Selected = true;
listViewRemos.Items[i].EnsureVisible();

If there is a chance the control does not have the focus but you want to force the focus to the control, then you can add the following line.

listViewRamos.Select();

Why Microsoft didn't just add a SelectItem() method that does all this for you is beyond me.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
9

I think that the problem and the solution was descripted by cody gray! I've an additional note.

Please check the focus of the specified listview item (and the control!). I could set the focus and the selection with the following lines of code :

this.listView1.Items[1].Selected = true;
this.listView1.Items[1].Focused = true;

But the focused control was a condition!

bitsmuggler
  • 1,689
  • 2
  • 17
  • 30
  • 1
    Setting the focus to an individual listview item is only necessary when you have *multiple* items selected. In that case, all of the selected items' backgrounds will be highlighted, but only one of them will have the dotted "focus rectangle". You can determine which one that is by setting the [`Focused` property](http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.focused.aspx), as you have done here. When only one item in the `ListView` is selected, it will always have the focus whenever its host control has the focus. – Cody Gray - on strike Apr 26 '11 at 14:00
4
        int i=99;//is what row you want to select and focus
        listViewRamos.FocusedItem = listViewRamos.Items[0];
        listViewRamos.Items[i].Selected = true;
        listViewRamos.Select();
        listViewRamos.EnsureVisible(i);//This is the trick
Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
1
if (listView1.Items.Count > 0)
{
    listView1.FocusedItem = listView1.Items[0];
    listView1.Items[0].Selected = true;
    listView1.Select();
}
0
ListViewItem.IsSelected = true;
ListViewItem.Focus();
Andy
  • 49,085
  • 60
  • 166
  • 233
user1724225
  • 37
  • 1
  • 4