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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListView - Refilling and Selecting

Status
Not open for further replies.

zandsc1

Programmer
Nov 12, 2008
53
US
I've read through the FAQ guides here and I'm trying to figure out a solution to what seems like it should be a simple problem. The users are looking at a priority list of jobs stored in a listview. I'm using a listview instead of a listbox because I want to be able to format each row based on the status of the job (i.e. make jobs that need repair red, make jobs that are on hold yellow, etc). I've got the listview populated fine and I can format it no problem. The issue is, when the user alters the status of a job then I need to requery the row source -- as far as I know the only way to do that is to clear the listview and refill it. Again, I can get this to work no problem (though it's slower than I'd like) but now I've lost the row that was selected since I cleared and refilled the listview. I'm looking around for a way to set the selecteditem because I can store the value of the selected row before I clear the listview and then just set it, but I can't find a read property of the listview that I can set after I refill it to select the same row again.

Code:
Public sub selectRow(mySelection as integer)

If myselection <> 0 Then 'something was selected and needs to be re-selected
[indent]Me.lsxPriorityList.SelectedItem (mySelection)[/indent]
End If

End Sub

This gives error "Object doesn't support this property or method"

I've also tried

Code:
Me.lsxPriorityList.SelectedItem = mySelection

which gives me a type mismatch

Suggestions? I'm hoping this is really easy and I'm just not used to listviews.


 
I found a solution and I wanted to follow up and post it.

First, I'm confirming that there is something selected in the listview and then storing that information:

Code:
Dim mySelection as integer

If Me.lsxPriorityList.SelectedItem Is Nothing Then
[indent]mySelection = 0[/indent]
Else
[indent]mySelection = Me.lsxPriorityList.SelectedItem[/indent]
End If

Note that if you don't first do a check to see that the selected item is nothing then you will get an error when you use the routine and nothing is clicked/selected in the listview.

Now that you have the index for the selected item, you can refresh all the data in your listview (I won't post how to do that here, it's available elsewhere) which will lose your place, and then reset it:

Code:
If mySelection <> 0 Then 
    Me.lsxPriorityList.ListItems(mySelection).selected = True
End If

So, as I'd hoped, it was an easy solution. Just had to hunt a while to find the right syntax.

Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top