选择一个项目在ListView控件

选择一个项目在ListView控件

本文介绍了选择一个项目在ListView控件(的WinForms),而没有重点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图模仿在Outlook地址簿的功能
所以基本上用户开始在编辑控制和匹配的ListView项一些文本输入选择

I am trying to mimic the functionality of the address book in OutlookSo basically a user starts typing in some text in an edit control and a matching ListView Item is selected

private void txtSearchText_TextChanged(object sender, EventArgs e)
{
   ListViewItem lvi =
            this.listViewContacts.FindItemWithText(this.txtSearchText.Text,true, 0);

        if (lvi != null)
        {
            listViewContacts.Items[lvi.Index].Selected = true;
            listViewContacts.Select();
        }

    }

一旦ListView项被选中着继续键入的文本框中,用户的问题,这是。基本上我想在列表视图的方式来突出显示的项目,同时仍保持专注于编辑控件

The problem with this is once the listview item gets selected the user cant keep typing into the text Box. Basically I want a way to highlight an item in the listview while still keeping the focus on the edit control

这是的WinForms 2.0

This is WINFORMS 2.0

推荐答案

手动设置 ListViewItem.BackColor 不是一个很好的解决方案,特别是如果你想要的项目,以获得状态,因为它只能在未选定项目。所以,你必须照顾几种情况,使它看起来正确在所有情况下(实际上只要在ListView获得焦点选择项目,撤消颜色变化,等等...)

Manually setting ListViewItem.BackColor is not good a solution, especially if you want the item to get the selected state, because it only works on unselected items. So you had to take care of several situations to make it look right in all cases (really select the item as soon as the ListView gets focus, undo the color changes, and so on...)

看来唯一的好方法是使用的OwnerDraw或扩展的ListView如。

It seems the only good way is to use Ownerdraw or an extended ListView like ObjectListView.

我一直在寻找同样的,我仍然希望有一个更好/更聪明的解决方案,或者至少是一个很好的和短的OwnerDraw执行。

I was looking for the same and I still hope for a better/smarter solution, or at least a nice and short Ownerdraw implementation.

更新结果
我找到了一个更好的解决方案的的:我现在使用 DataGridView的为同一目的(其中也有其他的优势,在我的情况下,由于数据来自分贝,无论如何,但它也没有DB工作)。有失去焦点时,选择条不变色。你可以尝试一些属性,使它看起来像一个ListView:

Update
I found a better solution for me: I now use a DataGridView for the same purpose (which also has other advantages in my case, since the data comes from a db anyway, but it would also work without db). There the selection bar doesn't change color when loosing focus. You can try a few properties to make it look like a ListView:

dgv.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
dgv.ColumnHeadersVisible = false;
dgv.MultiSelect = false;
dgv.ReadOnly = true;
dgv.RowHeadersVisible = false;
dgv.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
dgv.StandardTab = true;

这篇关于选择一个项目在ListView控件(的WinForms),而没有重点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:20