help keeping listview selection after refresh

Noremacam

Member
Joined
Jun 30, 2008
Messages
14
Programming Experience
1-3
I've searched the forum but couldn't find an answer, so I hope this isn't a duplicate. I have a listview(set as a details view) that is generated from a query to my database. I have a timer set to, every 5 minutes query the database and refresh the listview with a fresh copy, so that if someone else is on the database, the app stays relatively recent, which is satisfactory for what I'm using it for.

However whenever the listview gets repopulated with the new data, I lose which item I have selected.How do I go about having it remember which item was selected and then selecting that item again after refresh? There's no guarantee it'll be at the same location on the list, or that the item will still even be there(that's the only time it would make sense to have no selection after a refresh). If it helps, I'm not using MultiSelect, and every item name is guaranteed unique.

In short, how do I keep my selection highlighted after repopulating my listview?

Thanks in advance for any help you can give me!
 
Selection is only lost if you clear/remove the selected item, so if you just update by adding new and removing some the selection is preserved.

If you clear all and add all new you have to first find which unique item was selected and select this again afterwards. Then you could do this:
VB.NET:
Me.ListView1.Items("key").Selected = True
The "key" is the Name of each ListViewItem.
 
Thanks for the quick reply! However it seems to think, even after I fill the string that holds the item name, that the string is still null, and I'm not sure why.

VB.NET:
        Dim item As String
        If lstview.SelectedItems.Count > 0 Then
            item = lstview.SelectedItems.Item(0).Text
        End If

        SQLRefresh() ' my function that repopulates the listview

        If item = Not Nothing Then
            lstview.Items(item).Selected = True
            MessageBox.Show("I should be seeing a message box here but I don't")
        End If

This makes no sense to me. If I have the item named "2" selected in my listbox, it shows that customer becomes "2" in my quickwatch, but in spite of that, in the last if statement it doesn't execute(I checked the cheap way by putting a messagebox.show inside that last if statement, and it never showed).

Oh, I'm also aware this code doesn't check yet that the item still exists after SQLRefresh. I just haven't implemented that yet. Since I'm the only one using it, its not an issue.
 
Last edited:
"If item = Not Nothing Then" - VS say this code is wrong. Try "If item IsNot Nothing Then", but you don't need this either - you can use Items.ContainsKey to determine if the items contains a key before you try to select the item, it will work even if "item" string is not set.
 
I made the correction that JohnH suggested, but now I receive a NullReferenceException at the "lstview.Items(item).Selected = True" line. I've tried fiddling around with it a bit, but I can't seem to get rid of the exception.
 
I'm fairly sure that happens because "item" does not refer to a named ListViewItem, which causes Items(item) to return Nothing. Didn't you check first with Items.ContainsKey?
 
I'm fairly sure that happens because "item" does not refer to a named ListViewItem, which causes Items(item) to return Nothing. Didn't you check first with Items.ContainsKey?

Lets say my listview item name is "2". It successfully copies that variable to "item". ContainsKey(item) returns false. Very confusing. I'm pulling the name of the item into the "item" variable. Then it updates the listview. If I'm understanding you correct, you're saying that what's in "item"(in this example "2") doesn't exist in my listview anymore, but I have a problem with that, because my listview has not changed at all and I can visually see it still exists. The only way that could make any rational sense is if my syntax is wrong, or "key" is not analogous to the listview item name.

Basically right now my problem is simplified to selecting an item in a list based on its name. This feels so simple I can't believe its taking my so long :eek:
 
Last edited:
It is possible that you are confusing an items Name and it's Text ? Try this:
VB.NET:
Me.ListView1.Items.Add("some text").Name = "a name"
MsgBox("listview contains item by key ""a name"": " & Me.ListView1.Items.ContainsKey("a name"))
 
Sorry to be so late replying. Yes, embarrassingly, I confused the name with the text(which, was the "name" in a different context).

For whatever its worth, here's the code I found that fixed it, should anyone else be looking for a solution to this:

VB.NET:
Dim strItem As String = ""
If lstview.SelectedItems.Count > 0 Then
            strItem = lstview.SelectedItems.Item(0).Text 'determine selected item
End If

SQLRefresh() ' (or whatever code you have that repopulates the list view)

Dim Found As Boolean = False
Dim i As Integer = 0
If strItem IsNot "" Then
            Do Until Found = True Or i > lstCustomers.Items.Count 'until you find the right one, or you run out of items
                If lstview.Items.Item(i).Text = strItem Then
                    Found = True
                Else
                    i = i + 1
                End If
            Loop
            If Found = True Then
                lstview.Items(i).Selected = True
            End If
End If

I'm sure there's prettier ways to write the code, but this works for me.
 
Back
Top