select item in listview programmatically

R

Richard Bond

I'm having a lot of trouble programatically selecting a listview item. How
do you do this?

These didn't work for me in the context of the sub below (that's trying to
move up an Item in the listview)

MeasView.Items(ItemIndex - 1).Selected = True
MeasView.Items(ItemIndex - 1).Focused = True

I would really appreciate some help.

thank you,

Rich

Private Sub MoveUpButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MoveUpButton.Click
Dim ItemIndex As Integer

Select Case MeasView.SelectedIndices.Count
Case 0
MessageBox.Show("You must select a measure to move first")
Case Is > 1
MessageBox.Show("You must select just one measure to move")
Case 1
'Find the item and check it's index
ItemIndex = MeasView.SelectedIndices.Item(0)
If ItemIndex = 0 Then
MessageBox.Show("You can't move this item up any more")
Else
MeasView.Items(ItemIndex).Text = (ItemIndex -
1).ToString.PadLeft(2, "0")
MeasView.Items(ItemIndex - 1).Text =
(ItemIndex).ToString.PadLeft(2, "0")
MeasView.Sorting = SortOrder.None
MeasView.Sorting = SortOrder.Ascending
MeasView.Items(ItemIndex - 1).Selected = True
MeasView.Items(ItemIndex - 1).Focused = True
End If
End Select

End Sub
 
H

Herfried K. Wagner [MVP]

Hello,

Richard Bond said:
I'm having a lot of trouble programatically selecting a listview
item. How do you do this?

These didn't work for me in the context of the sub below
(that's trying to move up an Item in the listview)

Set the ListView's 'HideSelection' property to False.
 
R

Richard Bond

Thanks very much - That's done the trick - why did it not have focus in the
first place though?
 
O

One Handed Man [ OHM ]

Because when you clicked the button, the button received focus and the
listview was Blurred

Regards - OHM
 
R

Richard Bond

Hey that works aswell.The only difference being that the selection colour
changes from blue to grey

(no problem for me but my boss will tell me it will confuse the users)

thanks very much,

Rich
 

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