如何在程序中编程选择ListView中的项目?

82

我正在尝试通过编程方式选择ListView中的第一项,但似乎并没有被选中。 我正在使用以下代码:

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

其实我以前遇到过这个问题,但是我不记得我是怎么解决的了!

7个回答

94

很可能,该项已被选择,只是因为其他控件拥有焦点,所以你无法看到。根据你的应用程序设计,有几种不同的解决方法可供选择。

  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;
    

51
if (listView1.Items.Count > 0)
{
    listView1.Items[0].Selected = true;
    listView1.Select();
}

除非控件拥有焦点(或您将 HideSelection 属性设置为 false),否则列表项不会显示为选定状态。


4
没有解释的代码片段不算作答案。 - Cody Gray
8
但它回答了问题并且自我解释。 :-) - VikciaR
12
不,这并不是显而易见的。唯一有意义的方式是如果你明白列表项在控件没有焦点时不会显示为选定状态(或者设置了“HideSelection”属性为false)。如果你已经理解了这一点,那么你就不需要首先问这个问题。更重要的是,当用户在表单上点击其他地方时,这种方法会失效。突然间选定的项不再显示为选定状态!把你答案中的代码复制粘贴给别人的提问者将不知所措,并且他们将再次提出另一个问题。 - Cody Gray
2
好的,同意 - 你的回答更全面,更符合问题。 - VikciaR
如果元素不可见,您必须添加 UsersLst.Items[index].EnsureVisible(); 以显示该元素。 - Ruben Giaquinto

15

我知道这是一个老问题,但我认为这是最终的答案。

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

如果有可能控件没有焦点但你想强制让控件获得焦点,你可以添加以下代码。

listViewRamos.Select();

为什么微软不直接添加一个SelectItem()方法来代替这一切,这超出了我的理解范围。


这就是我一直在寻找的解决方案。其他的方案对我没用。 - César Javier Mendoza
所选的“Item”是什么类型?是“ListViewItem”吗?如果是,我无法使用“.”获取属性或方法。 - FredM

9

我认为问题和解决方案已经被Cody Gray描述得很清楚了!我有一个额外的注释。

请检查指定列表视图项(以及控件)的焦点。我可以通过以下代码设置焦点和选择:

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

但是专注的控制是必要的条件!

1
只有在选择了多个项目时,将焦点设置到单个列表视图项才是必要的。在这种情况下,所有选定的项目的背景都将被突出显示,但只有其中一个项目会有虚线“焦点矩形”。您可以通过设置Focused属性来确定哪个项目是这样的,就像您在这里所做的那样。当仅选择了ListView中的一个项目时,每当其宿主控件具有焦点时,它将始终具有焦点。 - Cody Gray

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

同意,EnsureVisible() 也同样重要。 - Steve Smith

1
if (listView1.Items.Count > 0)
{
    listView1.FocusedItem = listView1.Items[0];
    listView1.Items[0].Selected = true;
    listView1.Select();
}

0
ListViewItem.IsSelected = true;
ListViewItem.Focus();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接