470,573 Members | 1,653 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,573 developers. It's quick & easy.

Trouble Selecting a ListViewItem Programmatically

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

Nov 17 '05 #1
2 3549
See comment inline.

"Fritz" <fr**********@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.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

Nov 17 '05 #2

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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Marc W. | last post: by
4 posts views Thread by Paddy | last post: by
6 posts views Thread by grs | last post: by
2 posts views Thread by naikrovek | last post: by
reply views Thread by Kluch | last post: by
3 posts views Thread by larry mckay | last post: by
3 posts views Thread by Lars Netzel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.