请教一个关于组件开发方面的问题 ( 积分: 50 )

  • 主题发起人 主题发起人 awfigsk
  • 开始时间 开始时间
A

awfigsk

Unregistered / Unconfirmed
GUEST, unregistred user!
像下面这个组件的代码(部分):
type
TMdFontCombo = class(TComboBox)
private
{ Private declarations }
FChangeFormFont:Boolean;
procedure SetChangeFormFont(const Value:Boolean);
protected
{ Protected declarations }
public
{ Public declarations }
...
procedure Change;override;
published
{ Published declarations }
...
property ChangeFormFont:Boolean
read FChangeFormFont write SetChangeFormFont default True;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('MyComponent', [TMdFontCombo]);
end;

{ TMdFontCombo }

constructor TMdFontCombo.Create(AOwner: TComponent);
begin
inherited;
Style:=csDropDownList;
FChangeFormFont:=True;
end;

procedure TMdFontCombo.SetChangeFormFont(const Value: Boolean);
begin
FChangeFormFont:=Value;
if FChangeFormFont then //refresh font
Change;
end;

procedure TMdFontCombo.Change;
begin
//assign the font to the owner form
if FChangeFormFont and Assigned(Owner) and (Owner is TForm) then
TForm(Owner).Font.Name:=Text;
inherited;
end;
end.

上面的change方法对应着组件的onchange事件,当在使用该组件时,代码如下:
type
TForm1 = class(TForm)
Label1: TLabel;
Memo1: TMemo;
MdFontCombo1: TMdFontCombo;
procedure MdFontCombo1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.MdFontCombo1Change(Sender: TObject);
begin
end;

我不明白的是:我在使用组件时,在其change事件中并无代码,也无inherited关键字,为何在改变MdFontCombo1时,怎么会执行组件中的change方法的代码?而当[red]TForm1.MdFontCombo1Change中有代码时,则也会先执行TMdFontCombo.Change;中的代码,再执行程序中TForm1.MdFontCombo1Change代码?[/red]
 
像下面这个组件的代码(部分):
type
TMdFontCombo = class(TComboBox)
private
{ Private declarations }
FChangeFormFont:Boolean;
procedure SetChangeFormFont(const Value:Boolean);
protected
{ Protected declarations }
public
{ Public declarations }
...
procedure Change;override;
published
{ Published declarations }
...
property ChangeFormFont:Boolean
read FChangeFormFont write SetChangeFormFont default True;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('MyComponent', [TMdFontCombo]);
end;

{ TMdFontCombo }

constructor TMdFontCombo.Create(AOwner: TComponent);
begin
inherited;
Style:=csDropDownList;
FChangeFormFont:=True;
end;

procedure TMdFontCombo.SetChangeFormFont(const Value: Boolean);
begin
FChangeFormFont:=Value;
if FChangeFormFont then //refresh font
Change;
end;

procedure TMdFontCombo.Change;
begin
//assign the font to the owner form
if FChangeFormFont and Assigned(Owner) and (Owner is TForm) then
TForm(Owner).Font.Name:=Text;
inherited;
end;
end.

上面的change方法对应着组件的onchange事件,当在使用该组件时,代码如下:
type
TForm1 = class(TForm)
Label1: TLabel;
Memo1: TMemo;
MdFontCombo1: TMdFontCombo;
procedure MdFontCombo1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.MdFontCombo1Change(Sender: TObject);
begin
end;

我不明白的是:我在使用组件时,在其change事件中并无代码,也无inherited关键字,为何在改变MdFontCombo1时,怎么会执行组件中的change方法的代码?而当[red]TForm1.MdFontCombo1Change中有代码时,则也会先执行TMdFontCombo.Change;中的代码,再执行程序中TForm1.MdFontCombo1Change代码?[/red]
 
指定的OnChange事件和Change过程是不一样的,你看其父类的代码:
procedure TCustomComboBox.Change;
begin
inherited Changed;
if Assigned(FOnChange) then FOnChange(Self);
end;

执行过程是:父类的Change过程->父类的父类的Changed过程-->事件本身。
你可以单步调试一下:肯定是先跳转到TCustomComboBox的Change的begin处,然后
是你自己定义的事件的End处。
 
后退
顶部