Listview doesn't update selected index count

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
Anybody who has any idea on what to do here?

When I try to set an item to be selected programmatically, I have noticed that is only visually it is selected. If I set an item to be selected and clicks on the arrow down key, the listview seems to think that it is the first item in the listview which is selected and thereby it selects the item at index 1, nomatter what index I have selected
I have noticed that both dgvDamageReports.SelectedIndices.Count and dgvDamageReports.SelectedItems.Count returns 0, even though when I look at the Selected property on the item I have selected, it clearly returns true

I have the following code: (multiselect is set to false)

VB.NET:
lsvDamageReports.Items(3).Focused = True
lsvDamageReports.Items(3).Selected = True
lsvDamageReports.Items(3).EnsureVisible()
lsvDamageReports.Select()

lstDamageReports is a normal ListView. I have tried googling for a solution for the problem, but so far no luck

When running the above code, the watch window says the following:

VB.NET:
lsvDamageReports.Items(3).Selected = True
dgvDamageReports.SelectedItems.Count = 0

An item is clearly selected, but the ListView can't see that it is. Anybody who has any idea on what to do here?
 
I can't reproduce your error, here is what I did...

VB.NET:
        lsvDamageReports.Items(3).Focused = True
        lsvDamageReports.Items(3).Selected = True
        lsvDamageReports.Items(3).EnsureVisible()
        lsvDamageReports.Select()

        If lsvDamageReports.SelectedItems.Count > 0 Then
            MsgBox("Number of selected items: " & lsvDamageReports.SelectedItems.Count)
            Dim lvi As ListViewItem
            For Each lvi In lsvDamageReports.Items
                If lvi.Selected = True Then
                    MsgBox("Selected index is: " & lvi.Index)
                End If
            Next
        End If

and as expected it returned...

Number of selected items: 1
Selected index is: 3

Sorry. Good luck.
 
Thanks

After some more digging I could see the problem only occurred in my specific situation. I still haven't found an explanation, but at least I found a solution

The actual situation in my project was like this:
I had two forms, frmX and frmY. frmY is the form with the listview and frmX is a form that opens up frmY

frmX:

VB.NET:
dim newForm as new frmY
newForm.SelectItmInListView
newForm.ShowDialog

Which didn't work. I tried to change it to the following, in case the problem was because I tried to set the selected item before frmY was actually shown

VB.NET:
dim newForm as new frmY
newForm.Show
newForm.SelectItmInListView

But nope, that didn't help either

I also tried to set it in the constructor of frmY, but nope, no luck

My final solution was to make the selection during startup of form:

VB.NET:
public class frmY

private selectItemInListOnStartup as boolean 

public sub new(selectItemInList as boolean)
selectItemInListOnStartup = selectItemInList
end sub

private sub frmY_Load handles mybase.load
if selectItemInListOnStartup  = true
' Do the selection here
end if
end sub

end class

That's the only way I could make it work. I couldn't do it in the constructor and I couldn't do it after the frmY object has been built. I could only get it working in the from load sub
I still have no idea why it wouldn't work
 
Back
Top