我有一個包含 at TListBox 的表單,我在 onCreate 事件中填充它,我還在那里設定了所選專案。我希望串列框在表單顯示時顯示所選專案,因此我嘗試觸發該ScrollToItem方法。這不起作用。我也嘗試將它放入OnShow和OnActivate事件中,但它仍然不起作用。有沒有辦法讓它發揮作用?這是一個說明問題的示例程式:
`type
TForm5 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form5: TForm5;
implementation
{$R *.fmx}
procedure TForm5.FormCreate(Sender: TObject);
var
i: Integer;
lbi: TListBoxItem;
begin
for i := 1 to 50 do
begin
lbi := TListBoxItem.Create(ListBox1);
lbi.Text := 'item ' inttostr(i);
ListBox1.AddObject( lbi );
end;
ListBox1.itemindex := ListBox1.items.indexof('item 48');
ListBox1.ScrollToItem(ListBox1.Selected);
end;
end.`
和 FMX 檔案:
`object Form5: TForm5
Left = 0
Top = 0
Caption = 'Form5'
ClientHeight = 480
ClientWidth = 640
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
OnCreate = FormCreate
DesignerMasterStyle = 0
object ListBox1: TListBox
Position.X = 224.000000000000000000
Position.Y = 144.000000000000000000
TabOrder = 1
DisableFocusEffect = True
DefaultItemStyles.ItemStyle = ''
DefaultItemStyles.GroupHeaderStyle = ''
DefaultItemStyles.GroupFooterStyle = ''
Viewport.Width = 196.000000000000000000
Viewport.Height = 196.000000000000000000
end
end`
uj5u.com熱心網友回復:
TListBox有一個ViewportPosition: TPointF設定滾動條的屬性。設定后添加以下行ListBox1.ItemIndex:
ListBox1.ViewportPosition := PointF(0.0, ListBox1.itemindex * ListBox1.ItemHeight);
前一個假設所有專案都具有相同的高度(TListBox1.ItemHeight在物件檢查器或之前的代碼中設定)。您的FMX檔案沒有反映這一點,因此您可能需要添加它,否則不會發生滾動。
您可能希望為專案設定單獨的高度。在這種情況下,你必須遍歷所有專案最多要選擇一個你想要的,總結它們的高度,以獲得Y為期限ViewportPosition。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315599.html
