How to keep the selected row in listbox

Hi,

A listbox got refreshed with the updated values by a timer automatically.
How can I keep the selected row on listbox assuming that there is still the row selected after refreshing?

Thanks.

Can multiple rows be selected?

If only one then add a property SelectedRow as integer and save the selected row number there.

If more then use an array of integers: SelectedRows() as integer

Restore the selection from these values.

However, whenever listbox got refreshed, the rows are positioned in other row number.
Do I have to check the real value for selecting the row again?

[quote=178751:@changwon lee]However, whenever listbox got refreshed, the rows are positioned in other row number.
Do I have to check the real value for selecting the row again?[/quote]

You could set the row Rowtag as true so you can find it whatever the new order by scanning all the rows.

that is assuming he does not do a DELETEALLROWS, otherwise the rowtag values are lost

Yes. I always call ‘dataList.DeleteAllRows’ code before populating listbox.

Anything else I can try?

@changwon: you need to give more information on what exactly you are doing.

Where do the data come from? How do you refresh? What is the code?

[quote=178765:@changwon lee]Yes. I always call ‘dataList.DeleteAllRows’ code before populating listbox.

Anything else I can try?[/quote]
Lots of things. But without more info all we can do is guess how your app works. That just wastes everybodies time.

[quote=178765:@changwon lee]Yes. I always call ‘dataList.DeleteAllRows’ code before populating listbox.

Anything else I can try?[/quote]

You – could – Maybe – recognize a particular row by it’s content, assuming two rows cannot have the same content. Otherwise if you delete everything, I frankly do not see how the mother listbox could find her washed out kittens.

Assuming you populate from a database: store the info on if that record is selected in the database.

With the limited information we have, I would just add to the RowTag idea that you re-add the RowTag when you repopulate and then select the row based on the RowTag from before you cleared. Assuming you can use the RowTag as an ID for whatever the data is, and that the data is the same both times just sorted differently.

I just get data from Database and display those into the listbox.

Method: PopulateListbox(dataList As Listbox, rs As RecordSet)

If rs Is Nil Then Return

dataList.DeleteAllRows
dataList.Columncount = rs.Fieldcount

For i As Integer = 0 To rs.FieldCount-1
dataList.Heading(i) = rs.IdxField(i+1).Name
Next

While Not rs.EOF
dataList.AddRow("")
For i As Integer = 0 To rs.FieldCount-1
dataList.Cell(dataList.LastIndex, i) = rs.IdxField(i+1).StringValue
Next
rs.MoveNext
Wend

You could subclass the listbox and add a new property ‘lastselecteddata’ , or add a similar property to the containing window.
Of type string.

When a row is selected, set the lastselecteddata variable to be the string value of the selected row.
When you refresh the list , check the rows you add to see if they have the same string value as the lastselecteddata variable.
If they do, select the row

So why not just use Sort instead of clearing and reloading the ListBox?
Edit: I forgot the original post was for updated data.

So does your Database have a column you can use as a unique ID for each row? You could store the selected ID in memory before clearing the listbox, and then after the data is loaded again, select the row based on the ID you stored.

[quote=178774:@changwon lee]I just get data from Database and display those into the listbox.

Method: PopulateListbox(dataList As Listbox, rs As RecordSet)[/quote]
So you add different RecordSets to the Listbox? And a selected record might or might not be in the RecordSet again?

As I said add a boolean column Selected to your database which holds the info on wether that record was selected.

Then the procedure is:

Rows selected -> save info to database

New RecordSet -> if Record has Selected then select it in the listbox

Clear all the Selected in the database

Set the Selected again that are now selected

The data I get is very dynamic which means that the selected row will be in the listbox again but it is possible several rows couldn’t be there.

So, the only way I can do is just using database to store the Selected row value and the check it again with the update value?

You don’t need to store it in the database. Use an existing unique ID column from the database and store the selected row ID in memory. If it’s a SQLite database there’s one there even if you didn’t make one.

[quote=178780:@changwon lee]The data I get is very dynamic which means that the selected row will be in the listbox again but it is possible several rows couldn’t be there.

So, the only way I can do is just using database to store the Selected row value and the check it again with the update value?[/quote]

How do you know which card has been selected, if you are not even sure it is on the table ?

The data is Database Session information.
So, user would select the problematic session and would like to track the session detail information.
That’s why the selected row will be there for some time. The other rows(sessions) can be removed soon.
I think I should keep the property to store the session information user selects and compare it with the refreshed rows of the listbox.

Thanks for your great help.

Basic is the Best.

In Change event:

  If Me.listindex > -1 Then 'Selected
    
    selectedSID       = sessionList.Cell(sessionList.ListIndex,2)
    
  End If

In CellBackgroundPaint:

  If (Row >= 0 and Row < Me.ListCount) then
    
    Select Case me.Cell(row,2)
    Case selectedSID
      me.Selected(Row) = True
    End Select
    
  End If

It works well now.
Thank you so much!