关于写控件对象属性(50分)

  • 主题发起人 主题发起人 wenjk
  • 开始时间 开始时间
W

wenjk

Unregistered / Unconfirmed
GUEST, unregistred user!
各位朋友:
我写了一个控件,并该控件的属性“ErrorOutObject”为 TMemo 对象,一下代码是关于该属性的。但当我对属性“ErrorOutObject”设置为 Memo1 后,再选择该控件的其它属性,再删除 Memo1 时,提示错误。我想肯定是 SetErrorOutObject(VMemo: TMemo); 写得有问题,请大家帮帮忙。
不胜感激!
----------------------------------------
TCreateDB = class(TComponent)
private
FErrorOutObject :TMemo ;
procedure SetErrorOutObject(VMemo:TMemo);

published
property ErrorOutObject :TMemo read FErrorOutObject write SetErrorOutObject ;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TCreateDB]);
end;



procedure TCreateDB.SetErrorOutObject(VMemo: TMemo);
begin
try
if VMemo = nil then begin
VMemo.RemoveComponent(Self);
Exit ;
end else FErrorOutObject := VMemo ;
except
end;
end;
 
帮帮忙各位朋友
 
create 中添加
FErrorOutObject =:TMemo.create(nil);

destroy 中添加
FErroeOutObject.free;
 
To :jsxjd
你这方法好像不当好,因我的控件属性“ErrorOutObject”是在程序设计时进行设置的。
 
区分设计态和运行态,看看 Tlistbox 之类的。
 
重写控件的Notification过程。
procedure TComponent1.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
case Operation of
opRemove:
if Fconnection = nil then
exit;
end;
end;
给该控件赋值时需要调用事件FreeNotification。
procedure TComponent1.Setconnection(const Value: TadoConnection);
begin
if FConnection <> Value then
begin
FConnection := Value;
FConnection.FreeNotification(self);
end;
end;

 
你可不可以把你测试的代码贴出来呢。
 
还是不行,帮帮忙各位朋友
 
我上面的方法不行?
不会吧,
一般这种情况,按我说得写那两个过程就行了。
你再试试,别忘了更新包啊。
 
TO?book523,?我把实例单位全部拷贝如下:

unit Components;
interface
uses
Windows, Messages, SysUtils, Classes, StdCtrls;
type
TComponents = class(TComponent)
private
{ Private declarations }
FErrorOutObject :TMemo ; // 输出信息到 Memo 中
procedure Notification(AComponent: TComponent;
Operation: TOperation); OverLoad ;

procedure SetErrorOutObject(VMemo:TMemo);
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
property ErrorOutObject :TMemo read FErrorOutObject write SetErrorOutObject ;

end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TComponents]);
end;

procedure TComponents.SetErrorOutObject(VMemo: TMemo);
begin
{ try
if VMemo = nil then FErrorOutObject:=nil;
//if ErrorOutObject) =false then Exit ;
if FErrorOutObject =VMemo then Exit
else begin
FErrorOutObject :=VMemo ;
//FErrorOutObject.FreeNotification(Self);
end;
except
end; }
if FErrorOutObject <> VMemo then begin
FErrorOutObject := VMemo;
FErrorOutObject.FreeNotification(self);
end;
end;
procedure TComponents.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
case Operation of
opRemove: if FErrorOutObject = nil then exit;
end;
end;
end.
-----------------------------------
请教。
在控件 Components1 的属性“ErrorOutObject”连接 Memo1 的情况下,如果删除 Memo1 控件,或者多次连接、删除 Memo1 ,将出现错误提示。
 
请教,请教
 
你把测试例子贴出来吧,我都还没有听懂,怎样操作才会出错。
 
改一下
unit components;
interface
uses
Windows, Messages, SysUtils, Classes, StdCtrls;
type
TComponents = class(TComponent)
private
{ Private declarations }
FErrorOutObject :TMemo ; // 输出信息到 Memo 中
procedure SetErrorOutObject(VMemo:TMemo);
protected
procedure Notification(AComponent: TComponent;
Operation: TOperation); override ;
public
{ Public declarations }
published
{ Published declarations }
property ErrorOutObject :TMemo read FErrorOutObject write SetErrorOutObject ;

end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TComponents]);
end;

procedure TComponents.SetErrorOutObject(VMemo: TMemo);
begin
if VMemo <> FErrorOutObject then
begin
FErrorOutObject := VMemo;
if VMemo <> nil then VMemo.FreeNotification(Self);
end;
end;

procedure TComponents.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (AComponent = ErrorOutObject) then ErrorOutObject := nil;
end;
 
1:在控件 Components1 的属性“ErrorOutObject”中可以选择Tmemo 类的值。
2:当我选择控件 Components1 的属性“ErrorOutObject”下拉框的 Memo1 后,再选择控件 Components1 的其它属性(焦点移开“ErrorOutObject”)。
3:鼠标单击被控件 Components1 的属性“ErrorOutObject”选中的 Memo1 控件,按键盘的“Delete”键进行删除(将 Memo1 控件从界面上删除)。如此没有问题。
4:但再次在界面上新增 Memo1 控件(有可能出现错误),并将控件 Components1 的属性“ErrorOutObject”选择 Memo1 后就有可能出现错误了。
5:还有可以能在删除 Memo1 控件后新增 Memo1 到界面多次也可能出错。

请试试。。。。。
再次感谢!!!!
 
TO :realLearning,
对不起,还是有一点问题,就是:
当选择控件 Components1 的属性“ErrorOutObject”下拉框的 Memo1 后,可以立即从界面上删除 Memo1 控件;但如删除 Memo1 控件前将鼠标焦点移到控件 Components1 的属性 Tag 时,再删除 Memo1 控件就可能出错了。
请再帮忙看看,这种问题一般没有注意到的,我很多朋友他们在作控件时也没有注意该问题,都一时无法解决。
谢谢!!!!
 
看到你的一个错误:
procedure Notification(AComponent: TComponent;
Operation: TOperation); override ;//overload绝对不行。
我试了一下,改为override后就不会有问题了,不改就有问题啊。
看来你是笔误啊,结果还浪费这么多时间啊。
 
谢谢各位的帮助!!! [:)]
 
后退
顶部