Listbox Scrollposition

Hey all,

I found this bit of code on the forums a few weeks ago from @MarkusR i believe? (Cant find the post)

It loops through a listbox and highlights the row that matches the text in the textfield.

Textfield (TextChanged) event:

Pressed()

if TextField1.text = "" then
  Listbox1.SelectedRowIndex = ListBox1.NoSelection
end if

Two methods created:

Method 1 - Name = (Pressed)
Code:
Search TextField1.text

Method 2 - Name = (Search)
Code:

System.DebugLog CurrentMethodName

Var lb As Listbox = Listbox1

For row As Integer = 0 To lb.LastRowIndex
  Var found As Boolean = False
  For col As Integer = 0 To lb.ColumnCount-1
    Var s As String = lb.CellValueAt(row,col)
    If s.IndexOf(0, tx, ComparisonOptions.CaseInsensitive) >= 0 Then
      found = True
    End If
  Next
  lb.Selected(row) = found
Next

2021-10-01_12-57-26_SparkVideo

Video (Incase above gif too small)

As you can see from the gif above (Hopefully) when searching for a row that is out of view, I have to scroll manually to that position.

My question, is there anything I could add to the above code to do make it bring the selected row into view automatically?

Thank you in advanced!

Robin

This have an answer in this forum:

bring the selected row into view automatically

You only have to search for it ('cause I forgot where it was).

1 Like

Doh!

I added:

lb.scrollposition = (row)

to

Var lb As Listbox = Listbox1

For row As Integer = 0 To lb.LastRowIndex
  Var found As Boolean = False
  For col As Integer = 0 To lb.ColumnCount-1
    Var s As String = lb.CellValueAt(row,col)
    If s.IndexOf(0, tx, ComparisonOptions.CaseInsensitive) >= 0 Then
      found = True
    End If
  Next
  lb.Selected(row) = found
  lb.scrollposition = (row)
Next

Thanks Emile!

i would only jump by the first match.
and in the column search you can exit the for after found get true.
and define the found before the row loop.

1 Like

The question was about one selected Row.

That Selected Row can be displayed vertically centered.

yes, but the source show this “lb.scrollposition = (row)” in a loop of all rows.

OK, so I noticed it when I first read the entry.

Then, I read the explanation text and left the code…

Thank you; now I understand.

However, trying to display multiple selected Rows is not a good idea, especially if there are many rows between the first and last selections; this may lead to not display any Selected Row…