關於元件設計時,設定一個屬性連到其他元件的問題? |
答題得分者是:cmf
|
skurama
中階會員 發表:88 回覆:127 積分:73 註冊:2002-07-22 發送簡訊給我 |
請問:
目前我繼承TEdit,做了一個TEdit1,
在TEdit1中加入一個屬性 MyLabel 用來連結TLabel,
此時拉TLabel和TEdit1到Form上,
將Tedit1的 MyLabel屬性連結 Label1後,
再移除 Label1 元件,此時回到TEdit1時,MyLabel 屬性會被清空。 此時我又繼承TEdit1,做出TMyEditV2,
因為繼承的原因,所以TMyEditV2也會有MyLabel的屬性,
同樣的拉了一個TLabel和TMyEditV2到Form上,
也設定TMyEditV2的MyLabel屬性連到Label1,
但此時刪除Label1元件後,一回到TMyEditV2時,就發生Error~
好像是TMyEditV2找不到Label1而發生的記憶體錯誤, 請問為什麼我做的TEdit1,在移除Label1可以正常,
但是我做的TMyEditV2,在移除Label1後會Error? 我大概的架構如下
lv1 TEdit
|
lv2 TEdit1
|----------┐
| | |
lv3 TMyEditV2 TMyEditV3 TMyEditV4 因為TMyEditV2, V3, V4都要連到Label上,
所以屬性 MyLabel 是開在Tedit1,這樣所有後代都會有該屬性,
但是會發生上面所發生的錯誤,
如果把屬性移到lv3時,都會正常,但難保不會有lv4的元件
(沒有啦,主要是要了解為什麼在子元件才會發生這個問題,
有沒有解決方法) 程式碼如下 TEdit1
---------------------------------------------------
unit TEdit1; interface uses
SysUtils, Classes, Controls, StdCtrls, MyLabel; type
TMyEdit = class(TEdit)
private
PLabel : TLabel;
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
{ Public declarations }
published
property MyLabel : TLabel read PLabel write PLabel;
{ Published declarations }
end; procedure Register; implementation procedure Register;
begin
RegisterComponents('Young Net V3', [TMyEdit]);
end; { TMyEdit } constructor TMyEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner); end; destructor TMyEdit.Destroy;
begin inherited;
end; end.
----------------------------------------------------- MyEditV2 (基本上只有直接繼承)
-----------------------------------------------------
unit MyEditV2; interface uses
SysUtils, Classes, Controls, StdCtrls, TEdit1; type
TMyEditV2 = class(TMyEdit)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
end; procedure Register; implementation procedure Register;
begin
RegisterComponents('Young Net V3', [TMyEditV2]);
end; end.
----------------------------------------------------- ----------------
初出芧房程設師,
左鍵右鼠寫程式,
日扣夜寫眼框溼,
望能早成系分師。
----------------
------
---------------- 初出芧房程設師, 左鍵右鼠寫程式, 日扣夜寫眼框溼, 望能早成系分師。 ---------------- |
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
unit TEdit1; interface uses
SysUtils, Classes, Controls, StdCtrls, MyLabel; type
TMyEdit = class(TEdit)
private
PLabel : TLabel;
{ Private declarations }
protected
{ Protected declarations } procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
{ Public declarations }
published
property MyLabel : TLabel read PLabel write SetPLabel;
{ Published declarations }
end; procedure Register; implementation procedure Register;
begin
RegisterComponents('Young Net V3', [TMyEdit]);
end; { TMyEdit } constructor TMyEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
PLabel:=nil;
end; destructor TMyEdit.Destroy;
begin inherited;
end; Procedure TMyEdit.SetPLabel(Value:TLabel );
begin
PLabel=Value; if Assigned(PLabel) then
begin
Value.FreeNotification(Self);
end; end; procedure TMyEdit.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation); if Operation = opRemove then
if AComponent = PLabel then PLabel := nil
else if AComponent = Action then Action := nil;
end; end.
------
︿︿ |
skurama
中階會員 發表:88 回覆:127 積分:73 註冊:2002-07-22 發送簡訊給我 |
呼~~~好高深的解法,
看help看到一個頭兩個大,
請問我這樣解釋不知道是不是正確,
在
Procedure TMyEdit.SetPLabel(Value:TLabel );
begin
PLabel=Value;
if Assigned(PLabel) then
begin
Value.FreeNotification(Self);
end;
end;
是否說明使用者選擇的label,
如果Assigned不為nil時,
則呼叫其FreNotification的方法,
主要是用來通知MyEdit元件說,當選擇的Label即將被摧毀時,
要通知MyEdit元件。
這樣解釋對嗎? 而以下
procedure TMyEdit.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation); if Operation = opRemove then
if AComponent = PLabel then PLabel := nil
else if AComponent = Action then Action := nil;
end; 是否是指MyEdit所擁有的元件當被插入或移除時,則會執行此程序。
而在本元件所擁有的元件或屬性所連結的元件被移除時,
且該元件是PLabel時,則把目前的元件設定為nil,
至於
if AComponent = Action then
Action := nil;
是用來做什麼的,
因為我設定中斷點時,
好像只會判斷if AComponent = Action then
而都不會去做到Action := nil; 能不能麻煩在說明一次,謝謝~~~ ----------------
初出芧房程設師,
左鍵右鼠寫程式,
日扣夜寫眼框溼,
望能早成系分師。
----------------
------
---------------- 初出芧房程設師, 左鍵右鼠寫程式, 日扣夜寫眼框溼, 望能早成系分師。 ---------------- |
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
|
skurama
中階會員 發表:88 回覆:127 積分:73 註冊:2002-07-22 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |