ListView Problem

J

Jesper, Denmark

Hi,,

I've have a problem using the ListViewItem.Selected property.

Let's say that Items[2] has been selected using the mouse on the control.

If I then invoke some code doing this

listView.Items[5].Selected = true;

its as if this 'message' does not proporgate to all corners of the control.
The items[5] item will turn blue alrigth (indicating it has been selected)
and will also (as expected) be the only one that occurs in
listView.SelectedItems.

However.. if I press e.g. arrow up on the keyboard (while the control is in
focus), the selected item will be the one above items[2] i.e. items[1] and
not items[4] as I would expect.

I read on msdn that its important to set the call the controls Focus()
procedure (as below)before using ListViewItem.Selected, but it doesn't help.

ListView1.Focus();
ListView1.Items[0].Selected = true;

Also, If I use the above syntax to select a listviewitem, the control will
not automatically scroll to the location as it would using the up and down
arrow or mouse.

Is there a function that I need to call in order to make the control behave
in the same way wether I'm clicking on the 3rd listviewitem or invoke

ListView1.Items[2].Selected = true;

?????
I would realy appreciate any help on this issue, thank you.
Jesper.
 
M

Morten Wennevik [C# MVP]

Hi Jesper,

You were halfway there. It is true you need to set focus, but the focus in
question is not the ListView focus but rather the ListViewItem.Focused
property. Change your code to

ListView1.Items[0].Focused = true;
ListView1.Items[0].Selected = true;
 
J

Jesper, Denmark

Thank you very much, it works!!. I'm happy!!

Morten Wennevik said:
Hi Jesper,

You were halfway there. It is true you need to set focus, but the focus in
question is not the ListView focus but rather the ListViewItem.Focused
property. Change your code to

ListView1.Items[0].Focused = true;
ListView1.Items[0].Selected = true;

--
Happy Coding!
Morten Wennevik [C# MVP]


Jesper said:
Hi,,

I've have a problem using the ListViewItem.Selected property.

Let's say that Items[2] has been selected using the mouse on the control.

If I then invoke some code doing this

listView.Items[5].Selected = true;

its as if this 'message' does not proporgate to all corners of the control.
The items[5] item will turn blue alrigth (indicating it has been selected)
and will also (as expected) be the only one that occurs in
listView.SelectedItems.

However.. if I press e.g. arrow up on the keyboard (while the control is in
focus), the selected item will be the one above items[2] i.e. items[1] and
not items[4] as I would expect.

I read on msdn that its important to set the call the controls Focus()
procedure (as below)before using ListViewItem.Selected, but it doesn't help.

ListView1.Focus();
ListView1.Items[0].Selected = true;

Also, If I use the above syntax to select a listviewitem, the control will
not automatically scroll to the location as it would using the up and down
arrow or mouse.

Is there a function that I need to call in order to make the control behave
in the same way wether I'm clicking on the 3rd listviewitem or invoke

ListView1.Items[2].Selected = true;

?????
I would realy appreciate any help on this issue, thank you.
Jesper.
 

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