How can I vertically center a TListBox element?

So, I want to vertically center the element TListBox

(not TListView

).

I can use the property TopIndex

, but how do I do it all.

If there are fewer items, so the scrollbar is not displayed, no centering is required, only the default item selection will be very good.

Something like that:

Vertically Centered TListBox

+3


source to share


1 answer


enter image description here



//IF YOU WANT TO SELECT THE CENTER ITEM 
procedure TForm2.Center;
  var VisibleItems : Integer;
begin
  VisibleItems := ListBox1.ClientHeight div  ListBox1.ItemHeight;
  ListBox1.TopIndex := Trunc((ListBox1.Items.Count / 2) - (VisibleItems / 2));
  if ListBox1.Items.Count > VisibleItems then
    ListBox1.Selected[ListBox1.TopIndex + (VisibleItems div 2)] := True
  else
    ListBox1.Selected[ListBox1.Items.Count div 2] := True;
end;



//IF YOU WANT TO CENTER A ITEM
procedure TForm2.Center(Index : Integer);
  var VisibleItems : Integer;
begin
  VisibleItems := ListBox1.ClientHeight div ListBox1.ItemHeight;
  if Index > VisibleItems then
    ListBox1.TopIndex :=  Index - (VisibleItems div 2);
end;

      

+5


source







All Articles