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