Firemonkey列表框需要一个TopItem

问题描述 投票:0回答:1

我需要能够获取和设置列表框中的第一项-像ListBox.TopItem属性之类的东西会很棒。我一直找不到能完成这项工作的东西。任何想法都将不胜感激。

listbox firemonkey
1个回答
0
投票

既然如此,也许在我的评论中我没有为您提供足够清晰的交流,我提供了以下示例代码,以帮助您解决问题。 Button1用一些简单的项目填充ListBox1Button2通过读取Text属性并将其显示在Edit1中来回答您如何获得顶级商品的要求。最后,Button3通过将其索引设置为0,然后将所有其他项目下移,将所选项目移到ListBox1的顶部。

procedure TForm25.Button1Click(Sender: TObject);
var
  i: integer;
  LBItem: TListBoxItem;
begin
  for i := 0 to 4 do
  begin
    LBItem := TListBoxItem.Create(nil);
    LBItem.Parent := ListBox1;
    LBItem.Height := 40;
    LBItem.Text := 'Item '+IntToStr(i);
  end;
end;

procedure TForm25.Button2Click(Sender: TObject);
begin
  Edit1.Text := ListBox1.ItemByIndex(0).Text;
end;

procedure TForm25.Button3Click(Sender: TObject);
begin
  if ListBox1.Selected <> nil then
    ListBox1.Selected.Index := 0;
end;

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.