简体   繁体   English

删除运行时创建的TLabel

[英]Delete TLabel created in run-time

How to delete created labels. 如何删除创建的标签。 I tried FindComponent but failed , what I have to do? 我尝试了FindComponent但失败了,我该怎么办? should I set there parent to other component like TPanel or what? 我应该在那里将父级设置为TPanel等其他组件吗?

procedure TForm1.Button1Click(Sender: TObject);
var
  lblLink: TLabel;
begin
   for i := 0 to stringtList.Count-1 do
   begin 
     lblLink := TLabel.create(self);

     with lblLink do
     begin
       name:='lblLink'+inttostr(i);
       caption:inttostr(i);
       Parent := self;
       font.style := [fsUnderline];
       cursor := crHandPoint;
       color := clBlue;
       font.Color := clBlue;
     end;
   end;
end;

You can iterate over the Components property, then check for the name of the component and finally free the component. 您可以遍历Components属性,然后检查组件的名称并最终释放该组件。

Var
  LIndex : Integer;
  LComponent : TComponent;
begin
  for LIndex := ComponentCount-1 downto 0 do
    if StartsText('lblLink',Components[LIndex].Name) then
    begin
     LComponent:=Components[LIndex];
     FreeAndNil(LComponent);
    end;
end;

You don't have to free it. 您不必释放它。 You gave the responsibility to free it to the form with lblLink := TLabel.create(self); 您有责任使用lblLink := TLabel.create(self);将其释放到表单中lblLink := TLabel.create(self); . The form will free the label when the form is freed. 释放表单后,表单将释放标签。

However, with that being said, you can free it by looping through the form's Components array: 不过,您可以通过遍历表单的Components数组来释放它:

procedure TForm1.DeleteLabel(const LabelName: string);
var
  i: Integer;
begin
  for i := ComponentCount - 1 downto 0 do
  begin
    if Components[i] is TLabel then
      if Components[i].Name = LabelName then
      begin
        Components[i].Free;
        Break;
      end;
  end;
end;

You assigned both an Owner and a Parent to each TLabel , so techncally you do not need to free them at all. 您为每个TLabel分配了OwnerParent ,因此从技术上讲,您根本不需要释放它们。 Both the Owner and the Parent will handle that for you. 所有者和父母都会为您处理。 However, if you wanted to free them earlier, you could loop through the Owner's Components list or the Parent's Controls list, hunting for the labels manually. 但是,如果您想更早地释放它们,则可以遍历“所有者的Components列表或“父母的Controls列表,手动寻找标签。 A better option is to keep your own list of the labels you create, then you can loop through that list when needed, eg: 更好的选择是保留自己创建的标签列表,然后可以在需要时遍历该列表,例如:

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  ...
  private
    Labels: TList;
    procedure FreeLabels;
  ...
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Labels := TList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Labels.Free;
end;

procedure TForm1.FreeLabels;
var
  I: Integer;
begin
  for I := 0 to Labels.Count-1 do
    TLabel(Labels[I]).Free;
  Labels.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  lblLink : TLabel;
  ...
begin 
  ...
  for I := 0 to StringList.Count-1 do
  begin 
    lblLink := TLabel.Create(Self);
    try
      with lblLink do
      begin
        Name := 'lblLink' + IntToStr(i);
        Parent := Self;
        Caption := IntToStr(i);
        Font.Style := [fsUnderline];
        Cursor := crHandPoint;
        Color := clBlue;
        Font.Color := clBlue;
      end;
      Labels.Add(lblLink);
    except
      lblLink.Free;
      raise;
    end;
  end;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM