請教如何可以在程式運行中create及delete元件?? |
尚未結案
|
christy315
一般會員 發表:9 回覆:8 積分:3 註冊:2004-07-11 發送簡訊給我 |
|
change.jian
版主 發表:29 回覆:620 積分:439 註冊:2003-06-02 發送簡訊給我 |
hi,christy315:
如下:
procedure TForm1.BitBtn1Click(Sender: TObject); var aLabel:TLabel; begin aLabel:=TLabel.Create(Self); aLabel.Parent:=GroupBox1; aLabel.Top:=50; aLabel.Left:=50; aLabel.Caption:='這是產生的Label'; end;重點是aLabel.Parent:=GroupBox1這一行,這樣aLabel才會顯示出來 至於刪除,就是把aLabel.free就好.但要記得,動態產生的TLabel,要把變數儲存起來,後面要刪除才好free; |
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
1.如果元件的 Owner 是 Form1, 就用 Form1.FindComponent
如果元件的 Owner 是 GroupBox1, 就用 GroupBox1.FindComponent
假設要刪除 GroupBox1 中的 Image1 與 Label1, 例:
var img: TImage; lbl: TLabel; begin img := Form1.FindComponent('Image1') as TImage; // img := GroupBox1.FindComponent('Image1') as TImage; if img <> nil then FreeAndNil(img); // 刪除 Image1 lbl := Form1.FindComponent('Label1') as TLabel; // lbl := Form1.FindComponent('Label1') as TLabel; if lbl <> nil then FreeAndNil(lbl); // 刪除 Label1 end;也可用 GroupBox1.ControlCount 來做, 例: var i: integer; c: TControl; begin for i := GroupBox1.ControlCount - 1 downto 0 do begin c := nil; if (GroupBox1.Controls[i] is TImage) and (GroupBox1.Controls[i].Name = 'Image1') then begin c := GroupBox1.Controls[i]; FreeAndNil(c); end; if (GroupBox1.Controls[i] is TLabel) and (GroupBox1.Controls[i].Name = 'Label1') then begin c := GroupBox1.Controls[i]; FreeAndNil(c); end; end; end;2.在 GroupBox1 上 Create 一個 Label1 與 Image1: var img: TImage; lbl: TLabel; begin img := TImage.Create(GroupBox1); // img 的 Owner 為 GroupBox1 img.Parent := GroupBox1; img.Name := 'Image1'; img.Top := 5; img.Left := 30; lbl := TLabel.Create(GroupBox1); lbl.Parent := GroupBox1; lbl.Name := 'Label1'; lbl.Caption := 'Label1'; lbl.Top := 5; lbl.Left := 5; end;-- 歡迎光臨 KTop 研究院! |
richtop
資深會員 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
christy315 您好: 底下程式供您參考! 繼續加油!
我在 class="code">{$R *.dfm}
var
nLabel : integer = 1;
nImage : integer = 1; procedure TForm1.addLabelClick(Sender: TObject);
var
newlabel : TLabel;
begin
newlabel := TLabel.Create(GroupBox1);
newLabel.Parent := GroupBox1;
newlabel.Name := 'NewLabel' IntToStr(nLabel);
newlabel.Caption := newlabel.Name;
newlabel.Left := 10;
newlabel.Top := 20 nLabel * 15;
nLabel := nLabel 1;
end; procedure TForm1.addImageClick(Sender: TObject);
var
newImage : TImage;
begin
newImage := TImage.Create(GroupBox1);
newImage.Parent := GroupBox1;
newImage.Name := 'NewImage' IntToStr(nImage);
newImage.Left := 150;
newImage.Top := 10 (nImage-1) * 50;
newImage.Width := 100;
newImage.Height := 48;
newImage.Canvas.FillRect(newImage.ClientRect);
newImage.Canvas.TextOut(1,1,newImage.Name);
nImage := nImage 1;
end; procedure remove(controlName : AnsiString);
var
k : integer;
begin
for k:=0 to Form1.GroupBox1.ControlCount-1 do
begin
if ( Form1.GroupBox1.Controls[k].Name = controlName ) then
begin
Form1.GroupBox1.RemoveControl(Form1.GroupBox1.Controls[k]);
Exit;
end;
end;
end; procedure TForm1.RemoveControlClick(Sender: TObject);
begin
remove(Edit1.Text);
end; RichTop 敬上 =====*****
把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題!
=====#####
|
christy315
一般會員 發表:9 回覆:8 積分:3 註冊:2004-07-11 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |