Selecting A Specific Row In A ListBox In Code

  1. 2 weeks ago

    James R

    Nov 30 Pre-Release Testers, Xojo Pro Connecticut

    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?

  2. Greg O

    Nov 30 Xojo Inc

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

  3. Greg O

    Nov 30 Xojo Inc

    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.

  4. James R

    Nov 30 Pre-Release Testers, Xojo Pro Connecticut

    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

  5. Dave S

    Nov 30 San Diego, California USA

    me.listIndex=val(txtList.text)

  6. James R

    Nov 30 Pre-Release Testers, Xojo Pro Connecticut

    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.

  7. Tim H

    Nov 30 Pre-Release Testers Portland, OR USA
    Edited 2 weeks ago

    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?

  8. Greg O

    Nov 30 Xojo Inc
    Edited 2 weeks ago

    @James R 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

    Why not just store it in a variable?

    dim oldListIndex as Integer = List1.ListIndex

    And then restore with

    List1.ListIndex = oldListIndex
  9. James R

    Dec 1 Pre-Release Testers, Xojo Pro Connecticut

    @Greg OLone 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.

    @Tim H 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).

    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.

  10. Dave S

    Dec 1 San Diego, California USA

    @James R 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
  11. jim m

    Dec 1 Pre-Release Testers Phoenix, Arizona

    @James R I'm also not sure how to load a single row on a ListBox.

    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.

  12. Tim H

    Dec 1 Pre-Release Testers Portland, OR USA
    Edited 2 weeks ago

    @James R I'm also not sure how to load a single row on a ListBox.

    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

or Sign Up to reply!