• delphi怎么一次性动态删除(释放)数个动态创建的组件?


    比如procedure TForm1.Button1Click(Sender: TObject);
    var
    i:Integer;
    lbl: TLabel;
    begin
    for i:=1 to 3 do
    begin
    lbl:= TLabel.Create(Application);
    lbl.Parent := Self;
    lbl.Caption := 'lbl'+IntToStr(i);
    lbl.Top := 175;
    lbl.Height := 75;
    lbl.Width :=75 ;
    lbl.Left := i* lbl.Width + 10;
    end;
    动态生成了3个控件,但怎么在同一个事件中(再点一下这个按钮)就又把它们全都删除(也就是释放吧!)呢?

    --------------------------------------------------------

    用一个数组来存这些动态生成的指针,以便以后释放。

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;

    type
    TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    private
    { Private declarations }
    IsLableCreated:Boolean;
    Labels:array[0..2] of TLabel;
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    var
    I: Integer;
    begin
    if not IsLableCreated then
    begin
    for I := 0 to 2 do
    begin
    Labels[I]:=TLabel.Create(Self);
    with Labels[I] do
    begin
    Parent:=self;
    Caption := 'Label ' + IntToStr(I);
    Top := 175;
    Width := 75;
    Height :=75;
    Left := I*Width +10;
    end;
    IsLableCreated := True;
    end;
    end
    else
    begin
    for I := 0 to 2 do
    Labels[I].Free;
    IsLableCreated := False;
    end;
    end;

    end.

    ----------------------------------------------------------

    设置一个全局布尔变量 点一下改变其值
    var
    bnil: boolean=false;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    if bnil = false then
    //生成控件
    bnil := true;//改变值
    else //即bnil = true;
    //释放
    end;

  • 相关阅读:
    Codeforces 900D Unusual Sequences 容斥原理
    Codeforces 900C Remove Extra One 模拟
    Codeforces 897D. Ithea Plays With Chtholly (交互)
    Codeforces 895C
    Monkey测试环境搭建
    SDK/JDK,Shell/Shell脚本,Apache/APR ,MTK
    计算机基础——Java笔记一
    用Fiddler模拟低速网络环境(弱网)
    弱网定义
    Xcode中的几个常用文件路径
  • 原文地址:https://www.cnblogs.com/jijm123/p/11009022.html
Copyright © 2020-2023  润新知