Selecting an item in a ListView control ( winforms ) while not having the focus

前端 未结 2 649
时光取名叫无心
时光取名叫无心 2021-01-15 23:25

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 selec

相关标签:
2条回答
  • 2021-01-16 00:02

    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;
    
    0 讨论(0)
  • 2021-01-16 00:15

    ok never mind it's doable by just manipulating the background colour of the selected item

    0 讨论(0)
提交回复
热议问题