Ярослав Никольский отреагировал на
Равиль Зарипов (ZuBy) в Как получить доступ к выбранному элементу и свойствам listbox созданому в run time
5 мая, 2018
procedure TForm1.Button1Click(Sender: TObject);
var
AItem: TListBoxItem;
ALabel: TLabel;
i: integer;
begin
ListBox1.BeginUpdate;
for i := 0 to 100 do
begin
AItem := TListBoxItem.Create(ListBox1);
AItem.Parent := ListBox1;
AItem.Height := 80;
ALabel := TLabel.Create(AItem); // все контролы должны принадлежать AItem
ALabel.Parent := AItem; // а parent может быть другим
ALabel.Align := TAlignLayout.Top;
ALabel.Height := 25;
ALabel.Name := 'ALabelName' + Inttostr(i);
ALabel.StyledSettings := [TStyledSetting.Family, TStyledSetting.Style];
ALabel.TextSettings.Font.Size := 20;
ALabel.TextSettings.FontColor := TAlphaColorRec.Black;
ALabel.Text := Inttostr(Random(100));
end;
ListBox1.EndUpdate;
end;
procedure TForm1.ListBox1ItemClick(const Sender: TCustomListBox; const Item: TListBoxItem);
begin
if (Item.FindComponent('ALabelName' + Inttostr(Item.Index)) is TLabel) then
Caption := (Item.FindComponent('ALabelName' + Inttostr(Item.Index)) as TLabel).Text;
end;