Trouble Selecting a ListViewItem Programmatically

F

Fritz

I know it, I know it. It's come up a lot. I've done the Googling, but
the standard answer doesn't do what I expect it to. For the record, the
standard answer is: "Set the Selected property of a ListViewItem to
true." I also know from MSDN that the selection won't really take
effect until the ListView has focus.

My specific problem is that setting item.Select = true doesn't actually
select my item (or at least, doesn't highlight it). I'm implementing a
search feature, and the highlighting is kind of important.

Even more curious, Microsoft's own C# code sample at
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/P_System_Windows_Forms_ListViewItem_Selected.htm
(might need to get there through VS2005 documentation) illustrates my
problem exactly. The first list item does not get selected on list
population, even though the code tells it to do so. Selection seems to
work after a click on the button, but not before.

1) What's so magical about that button click?
2) Is this a bug in the framework?

Spanks

ff
 
N

Norman Yuan

See comment inline.

Fritz said:
I know it, I know it. It's come up a lot. I've done the Googling, but
the standard answer doesn't do what I expect it to. For the record, the
standard answer is: "Set the Selected property of a ListViewItem to
true." I also know from MSDN that the selection won't really take
effect until the ListView has focus.

My specific problem is that setting item.Select = true doesn't actually
select my item (or at least, doesn't highlight it). I'm implementing a

Yes, it is indeed SELECTED! However, wether the selected item is
highlighted or not is another question. ListView has a property called
"HideSelection", which is default to TRUE, meaning, if the listview does not
have focus, the selected item(s) do not get highlighted.

So, your options are:
1. Set HideSelection to False. In this case, if the listview does not have
focus, the selected item(s) are highlighted in gray color (not usual blue);
2. Make sure the listview get focus when you need to show selection of the
listview.
 
F

Fritz

Norman said:
Yes, it is indeed SELECTED! However, wether the selected item is
highlighted or not is another question. ListView has a property called
"HideSelection", which is default to TRUE, meaning, if the listview does not
have focus, the selected item(s) do not get highlighted.

Woohoo!! Thank you very much! The HideSelection property was the
answer.

That's been chafing me for days. If we're ever at happy hour together,
your next round's on me.

ff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top