How to position a row at the center of listbox


There are 2 listbox called a Left-list and a Right-list.
After retrieving database information(table_name) into Left-list & Right-list, I click any row in Left-list. With ‘Change’ event of Left-list, I get the table_name and want to check if there is the same table in Right-list.

Until this point, it works.

  For i As integer = 0 To TGTTableViewerList.ListCount - 1
    TGT_TableName = TGTTableViewerList.Cell(i,2)
    If table_name = TGT_TableName Then
      TGTTableViewerList.ListIndex =i
    End If

In case there are 2,000 tables in Right-list, the matched(selected) row is displayed mostly at the bottom of Right-list.

Do you have an idea to show the row at the center of the listbox(Right-list)?
“Center” means the center of the current view of the listbox, it is not a center of the total row of the listbox.

Thanks in advance.


a. you know the LustBox Height,
b. You know the Row Height (check Language Reference [LR])
c. You know the Row # you want to display at the vertical center of the Listbox,
d. You have a vertical ScrollBar (LR) that you can change its value.

With these, I think you can compute the Row position, so selectt that Row (ListIndex -_> LR] and set the ScrollBar vertical position using values in step a and b…)

I hope I forgot nothing.



was is enough ?

Other answer: ask in the Pro channel of this forum.

Emile Schwarz:

Light on from your great idea. Am trying to implement it but as a workaround, I just narrow down the number of rows of listbox, so easy to find the selected row.

Once it is implemented, I will get you posted.

Thank you so much.

Not sure if you want to center the selected row or any row. Either way, if you calculate the number of rows displayed, you can use Scrollposition minus half of the visible rows.

Here is an Extend:
(Place it in your Extends Module and call it using: ListBox1.CenterSelection)

Sub CenterSelection(Extends lbx As Listbox)
  // Try to Center Selected Row
  Dim iRowsVisible, iHeaderHeight As Integer
  // Adjust if there's a heading
  If lbx.HasHeading Then
    iHeaderHeight = lbx.HeaderHeight
  End If
  // Rows that are fully visible
  iRowsVisible = ((lbx.Height - iHeaderHeight) \\ lbx.RowHeight) - 1
  // Adjust ScrollPosition
  If lbx.ListIndex > -1 And lbx.ListCount > iRowsVisible \\ 2 Then
    lbx.ScrollPosition = lbx.ListIndex - (iRowsVisible \\ 2)
  End If
End Sub

I was digging into this logic but felt very hard.

I am dancing now.
It really works and what I wanted to find!
For the selected rows, center position of the visible rows.

Thank you so much.

Hi changwon,

after reading the last two entries, (and forgot the why of the thing), I read back your original post and find your idea… brilliant!

I certainly did not understand it at first / was on a hurry when I wrote my first answer.

Brilliant idea: why did it does not comes to me ?
I too had resulting Row at the top / bottom (first/last row of the window) and (for the bottom row) was not happy 'cause the reading is less easy.

Centering the result Row of a search is what have to be done (with exceptionswhen the result is near the top / end of the Listbox).

Thank you changwon for the idea, Marco for the answer. I will implement it asap !
(of course on a single search: I have a search all occurences and there it does not make sense)