Delphi 如何垂直居中TListBox项目?

Delphi 如何垂直居中TListBox项目?,delphi,listbox,centering,tlistbox,Delphi,Listbox,Centering,Tlistbox,因此,我想垂直居中TListBox(而不是TListView)项 我可以使用TopIndex属性,但是我该如何做呢 如果项目较少,因此滚动条不会出现,则不需要居中,仅选择默认项目即可 大概是这样的: 是否有必要计算浮点值?div不够吗?此外,我认为OP不想选择垂直居中的项目,而是想将所选项目垂直居中。关于div,你是对的,我更改了部分代码。如果他想将所选项目垂直居中,而不是选择垂直居中的项目,我将更改或删除此答案。@Kohull谢谢! //IF YOU WANT TO SELECT THE C

因此,我想垂直居中
TListBox
(而不是
TListView
)项

我可以使用
TopIndex
属性,但是我该如何做呢

如果项目较少,因此滚动条不会出现,则不需要居中,仅选择默认项目即可

大概是这样的:


是否有必要计算浮点值?
div
不够吗?此外,我认为OP不想选择垂直居中的项目,而是想将所选项目垂直居中。关于div,你是对的,我更改了部分代码。如果他想将所选项目垂直居中,而不是选择垂直居中的项目,我将更改或删除此答案。@Kohull谢谢!
//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;