I am trying to mimic the functionality of the address book in Outlook So 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();
}
}
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
This is WINFORMS 2.0
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...)
It seems the only good way is to use Ownerdraw or an extended ListView like ObjectListView.
I was looking for the same and I still hope for a better/smarter solution, or at least a nice and short Ownerdraw implementation.
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;
ok never mind it's doable by just manipulating the background colour of the selected item
精彩评论