3
所以我想垂直居中TListBox
(不是TListView
)项目。如何垂直居中TListBox项目?
我可以使用TopIndex
属性,但我该怎么做整件事。
如果项目较少,所以滚动条不出现,那么不需要居中,只有默认项目的选择才会正常。
事情是这样的:
所以我想垂直居中TListBox
(不是TListView
)项目。如何垂直居中TListBox项目?
我可以使用TopIndex
属性,但我该怎么做整件事。
如果项目较少,所以滚动条不出现,那么不需要居中,只有默认项目的选择才会正常。
事情是这样的:
//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;
是否有必要,计算浮点值? 'div'不够吗? –
此外,我不认为OP想要选择垂直中心项目,而是选择垂直居中的项目。 –
你对div的看法是正确的,我改变了代码的一些部分。如果他想要垂直居中选择的项目,而不是选择垂直中心项目,我会更改或删除此答案。 – Kohull