Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListView selection bug?

Status
Not open for further replies.

hansaplast

Programmer
Dec 17, 2001
61
NL
The selection anchor in a listview is not resetted when SelectedItems.Clear is called.

Place a ListView with a few items on a from. Next, set ListView.View = "Details". Add a button and add the folowing code
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim idx = Me.ListView1.FocusedItem.Index
    If idx < Me.ListView1.Items.Count - 1 Then idx = idx + 1
    Me.ListView1.SelectedItems.Clear()
    Me.ListView1.Items(idx).Selected = True
    Me.ListView1.Items(idx).Focused = True
    Me.ListView1.Focus()
End Sub
Select two or more (not all) items and press the button. After it is pressed hold SHIFT and DOWN arrow. In my case the previously selected items (before pressing the button) are reselected. How is this possible and is there a way to reset the selection anchor?

Whatever you do will be insignificant, but it is very important that you do it. (Mahatma Gandhi)
 
You need to change the FocusedItem property.

Code:
ListView1.SelectedItems.Clear()
ListView1.FocusedItem = Nothing
ListView1.Items.Item(2).Selected = True
ListView1.FocusedItem = ListView1.Items.Item(2)
 
Which version of VS are you using? I tested and copied that code from VS 2005 IDE.
 
Man, that does look like a bug in 2003. I guess that's why they changed it in 2005. I'll see if there is a work around.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top