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

Re: Trouble Selecting a ListViewItem Programmatically by Norman

Norman
Tue Nov 08 13:23:29 CST 2005

See comment inline.

"Fritz" <fritz_foetzl@hotmail.com> wrote in message
news:1131475198.655360.97340@f14g2000cwb.googlegroups.com...
>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.


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



Re: Trouble Selecting a ListViewItem Programmatically by Fritz

Fritz
Tue Nov 08 14:21:37 CST 2005


Norman Yuan wrote:

> 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