sw4all 0 Posted October 5, 2019 (edited) Hi to all. I have created TButton on the form and 2x TLabel in it. When clicking TButton I need to change the text in one TLabel. myButton is created on runtime. type TfrmMain = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } procedure newButtonClick(Sender: TObject); procedure newProductButtonClick(Sender: TObject); { Public declarations } end; implementation procedure TfrmMain.newProductButtonClick(Sender: TObject); var comp: TComponent; begin FlowLayout1.BeginUpdate; // find component comp := FindComponent('LabelProductPrice'+TButton(Sender).Name); if (comp is TLabel) then begin (comp as TLabel).Text := IntToStr(StrToInt((comp as TLabel).Text) +1); end else begin // create new button newButton := TButton.Create(Self); newButton.Parent := FlowLayout1; newButton.Name := 'btn'+TButton(Sender).Name; newButton.Text := ''; newButton.Hint := '1'; newButton.Width := FlowLayout1.Width; newButton.Height := 80; newButton.OnClick := newButtonClick; newButton.Tag := TButton(Sender).Tag; newButton.OnMouseEnter := newButtonMouseEnter; newButton.OnMouseMove := newButtonMouseMove; newButton.OnMouseLeave := newButtonMouseLeave; newButton.Width := FlowLayout1.Width -3; newButton.OnClick := newButtonClick; LabelProductName := TLabel.Create(Self); LabelProductName.Parent := newButton; LabelProductName.Align := TAlignLayout.Top; LabelProductName.Text := TButton(Sender).Text; LabelProductName.Hint := newButton.Hint; LabelProductName.Margins.Left := 3; LabelProductName.Margins.Right := 3; LabelProductName.Margins.Bottom := 3; LabelProductName.Margins.Top := 3; LabelProductName.TextAlign := TTextALign.Center; LabelProductName.Name := 'LabelOne'+TButton(Sender).Name; LabelProductName.AutoSize := True; LabelProductName.TextSettings.FontColor := TAlphaColors.Red; LabelProductName.StyledSettings := LabelProductName.StyledSettings - [TStyledSetting.Family, TStyledSetting.Size,TStyledSetting.FontColor]; LabelProductName.OnClick := newButtonClick; LabelProductPrice := TLabel.Create(Self); LabelProductPrice.Parent := newButton; LabelProductPrice.Align := TAlignLayout.Bottom; LabelProductPrice.Text := TButton(Sender).Hint; LabelProductPrice.Margins.Left := 3; LabelProductPrice.Margins.Right := 3; LabelProductPrice.Margins.Bottom := 3; LabelProductPrice.Margins.Top := 3; LabelProductPrice.AutoSize := True; LabelProductPrice.TextAlign := TTextALign.Center; LabelProductPrice.Name := 'LabelTwo'+TButton(Sender).Name; LabelProductPrice.Tag := TButton(Sender).Tag; LabelProductPrice.OnClick := newButtonClick; end; FlowLayout1.EndUpdate; end; procedure TfrmMain.FormCreate(Sender: TObject); var i: integer; begin for i := 10 downto 1 do begin ProductButton := TButton.Create(Self); ProductButton.Parent := Panel1; ProductButton.Name := 'btn'+i.ToString; ProductButton.Text := 'product '+i.ToString; ProductButton.Hint := i.ToString; ProductButton.Height := 80; ProductButton.Width := Panel1.Width -3; ProductButton.Align := TAlignLayout.Top; ProductButton.Tag := i; ProductButton.OnClick := newProductButtonClick; end; end; procedure TfrmMain.myButtonClick(Sender: TObject); var comp: TComponent; begin comp := FindComponent('LabelTwo'+TButton(Sender).Tag.ToString); if (comp is TLabel) then begin TLabel(comp).Text := IntToStr(StrToInt((comp as TLabel).Text) -1); end; end; Thank you. Edited October 5, 2019 by sw4all Share this post Link to post
Dave Nottage 557 Posted October 6, 2019 You're naming the labels as (for example): 'LabelTwo'+TButton(Sender).Name but you're calling FindComponent as: FindComponent('LabelTwo'+TButton(Sender).Tag.ToString) When it should be: FindComponent('LabelTwo'+TButton(Sender).Name) Share this post Link to post