select item in listview programmatically

  • Thread starter Thread starter Richard Bond
  • Start date Start date
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
 
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.
 
Thanks very much - That's done the trick - why did it not have focus in the
first place though?
 
Because when you clicked the button, the button received focus and the
listview was Blurred

Regards - OHM
 
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
 
Back
Top