Selecting A Specific Row In A ListBox In Code

I have an app that loads quite a bit of information in multiple lines in a single row of a ListBox. When you double-click on a row a new window opens and you do stuff in that window. When you exit the window to the previous window, I reload the ListBox with the updated information, but when I do that, the row I previously selected is no longer selected.

I was wondering if there was a way in code to record the current row before moving to the new window and then select that row in code when you return?

if the rows aren’t changing order, just store the listindex before you call DeleteAllRows and then set it back when you’re done.

If you allow users to change the sort column and direction, you’ll need to store/restore those values too, in which case, you may want to store a unique identifier for the selected row(s) and then reselect them as you reload the data.

Hi Greg,

I can store the ListIndex in a TextBox with this code.

txtList.Text= Str(Me.ListIndex)

I just don’t know how to call it back so that the Listbox selects the ListIndex stored in txtList

me.listIndex=val(txtList.text)

Thanks Dave,

I just figured that out a couple seconds before you posted this.

List1.ListIndex=Val(txtList.Text)

Thanks for your help. I appreciate it.

Also record the ScrollPosition and restore that so the row will be in view (if setting ListIndex doesn’t scroll there, I don’t recall if it does).

Edit: is it necessary to reload the entire listbox? Could you just reload the one row?

[quote=416360:@James Redway]Hi Greg,

I can store the ListIndex in a TextBox with this code.

txtList.Text= Str(Me.ListIndex)

I just don’t know how to call it back so that the Listbox selects the ListIndex stored in txtList[/quote]
Why not just store it in a variable?

dim oldListIndex as Integer = List1.ListIndex

And then restore with

List1.ListIndex = oldListIndex

[quote=416384:@Greg O’Lone]Why not just store it in a variable?

dim oldListIndex as Integer = List1.ListIndex

And then restore with

List1.ListIndex = oldListIndex

Thanks Greg. I needed up doing that. I appreciate the help.

Hi Tim,

The scroll position of the ListBox does not change when reloaded, so it looks okay. I’m also not sure how to load a single row on a ListBox.

listbox1.cell(row,col)=newValue // update value in one cell
listbox1.invalidatecell(row,col) // force redraw of ONLY that cell

If you only need to update the row that is selected, you don’t need to worry about setting the selection or position.

Rather than reload the entire list, you could loop through the columns and set each cell or pass a tab-delimited string. ie

list1.cell( list1.listindex , -1 ) = join( newvalues() , chr(9) )
where newvalues() is an array of values to populate the selected row.

From the Docs:

row and column are zero-based. The top-left cell is 0,0. Passing -1 as either the Row or Column number means all rows or all columns, respectively. For example, the following specifies all columns in the last row added using AddRow or InsertRow:

Me.Cell(Me.LastIndex, -1)
If you set this equal to a tab-delimited string, you can update the row with one line of code.

I presume you have code that takes the values of the row and populates the fields in the second window. The code to reload the row would be the reverse of that.

// in window1
window2.editfield1.text = listbox1.cell(listbox1.listindex, 0)
window2.editfield2.text = listbox1.cell(listbox1.listindex, 1)
// in window2
window1.listbox1.cell(window1.listbox1.listindex, 0) = editfield1.text
window1.listbox1.cell(window1.listbox1.listindex, 1) = editfield2.text