線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:999
推到 Plurk!
推到 Facebook!

如何利用一個procedure或function修改其他form內的元件屬性

答題得分者是:deity
catvboy
一般會員


發表:3
回覆:9
積分:2
註冊:2005-04-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-06-29 10:40:41 IP:210.243.xxx.xxx 未訂閱
想請問各位,我想在form1中寫一個procedure或function,這個procedure或function可以傳入欲修改元件屬性的form名稱(例如form3),這樣就可以在form3或其他form中直接使用form1的這個procedure或function,該如何做呢?? //只要是TLabel的元件,其字型顏色為黑色 procedure Tfrm_Main.chageFONT(actForm: TForm); var i:integer; begin for i:=0 to actForm.Componentcount-1 do begin if actForm.Components[i].ClassName='TLabel' then actForm.TLabel(actForm.Components[i]).Font.Color := 'clWindowText'; end; end; 程式在這一行出現TLabel未定義 actForm.TLabel(actForm.Components[i]).Font.Color := 'clWindowText'; 可是actForm.Components[i].ClassName抓出來是TLabel啊 還是各位有更好的解法??????
supman
尊榮會員


發表:29
回覆:770
積分:924
註冊:2002-04-22

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-06-29 11:34:56 IP:61.70.xxx.xxx 未訂閱
您好: 我改了一下,但不是Function,您要自己在修改一下. procedure TForm1.Button4Click(Sender: TObject); var i:integer; begin for i:=0 to Form1.ComponentCount-1 do begin if Form1.Components[i].ClassName='TLabel' then TLabel(Form1.Components[i]).Font.Color:=clWindowText; end; end; 有兩個地方錯了. 1.Form.TLabel.....這個是錯誤的Components是TForm的屬性才對. 2.clWindowsText是常數值,不是字串. 發表人 - supman 於 2005/06/29 12:01:38
deity
尊榮會員


發表:90
回覆:876
積分:678
註冊:2003-05-09

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-06-29 11:48:03 IP:218.15.xxx.xxx 未訂閱
引言: 想請問各位,我想在form1中寫一個procedure或function,這個procedure或function可以傳入欲修改元件屬性的form名稱(例如form3),這樣就可以在form3或其他form中直接使用form1的這個procedure或function,該如何做呢??
//只要是TLabel的元件,其字型顏色為黑色
procedure Tfrm_Main.chageFONT(actForm: TForm);
var i:integer;
begin 
   for i:=0 to actForm.Componentcount-1 do
     begin
     if actForm.Components[i].ClassName='TLabel' then
       actForm.TLabel(actForm.Components[i]).Font.Color := 'clWindowText';
   end;
end;
改为如下:
procedure TForm1.chageFONT(actForm: TForm);
var
  i:integer;
begin
  for i:=0 to self.Componentcount-1 do
    begin
      if self.Components[i].ClassName='TLabel' then
        TLabel(actForm.Components[i]).Font.Color :=clWindowText;
    end;
end;
本Form的调用
procedure TForm1.Button1Click(Sender: TObject);
begin
  chageFONT(self);//或chageFONT(Form1); self当前父类!
end;
其他Form的调用
procedure TForm2.Button1Click(Sender: TObject);
begin
 Form1.chageFONT(self);//或 Form1.chageFONT(Form2);
end;    
程式在這一行出現TLabel未定義 actForm.TLabel(actForm.Components[i]).Font.Color := 'clWindowText'; 可是actForm.Components[i].ClassName抓出來是TLabel啊 還是各位有更好的解法??????
============================ 为什么经过多年以后,得失的过程如此冷漠 ============================ 發表人 - deity 於 2005/06/29 11:51:09
catvboy
一般會員


發表:3
回覆:9
積分:2
註冊:2005-04-12

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-06-29 14:24:11 IP:210.243.xxx.xxx 未訂閱
按照deity的作法,已經可以了 也感謝supman的指導 謝謝
catvboy
一般會員


發表:3
回覆:9
積分:2
註冊:2005-04-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-06-29 16:40:22 IP:210.243.xxx.xxx 未訂閱
引言: 按照deity的作法,已經可以了 也感謝supman的指導 謝謝
我修正一下,應該這樣才對 procedure TForm1.chageFONT(actForm: TForm); var i:integer; begin for i:=0 to actForm.Componentcount-1 do begin if actForm.Components[i].ClassName='TLabel' then TLabel(actForm.Components[i]).Font.Color :=clWindowText; end; end; 因為用self的話,元件會變成父視窗內的元件
系統時間:2024-09-08 10:46:08
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!