简体   繁体   中英

Delete TLabel created in run-time

How to delete created labels. I tried FindComponent but failed , what I have to do? should I set there parent to other component like TPanel or what?

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.

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); . 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:

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. 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. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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