如何选择一个ListView编程的项目? [英] How to select an item in a ListView programmatically?

查看:183
本文介绍了如何选择一个ListView编程的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式选择在ListView中的第一项,但能够被选中它不会出现。我使用下面的code:

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!

推荐答案

最有可能的,该项目的的被选中,你就不能说,因为不同的控制具有焦点。有一对夫妇,你可以解决这个不同的方式,根据您的应用程序的设计。

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. 简单的解决方案是将焦点设置为的ListView 第一每当显示表单。用户通常通过点击设置重点控制。但是,您也可以指定控制获得焦点编程。这样做的一种方式是通过设置控制的为0的标签指数(最低值表示将有初始聚焦控制)。第二种可能性是使用code的下面一行在窗体的加载事件或后立即将属性:

  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).

要解决这个问题,你将需要设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.hideselection.aspx\"><$c$c>HideSelection的ListView 控制为False财产。这将导致所选项目的仍然强调的,甚至当控件失去焦点。

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.

您可以设置该属性要么在设计时,也可以通过code:

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

myListView.HideSelection = false;


这篇关于如何选择一个ListView编程的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆