为什么我编写的控件的属性值设置不能显示在设计界面中?(200分)

  • 主题发起人 主题发起人 fei_xr
  • 开始时间 开始时间
F

fei_xr

Unregistered / Unconfirmed
GUEST, unregistred user!
急死我了,我编写一个继承自TEdit的控件,增加一个属性“HeadNum”,希望在设计时通过Object Inspector进行设置,设置的值显示的编辑框中,可我写的代码不能显示值,却显示控件的名称:
unit BodyCard;
interface
uses
SysUtils, Classes, Controls, StdCtrls;
type
TBodyCard = class(TEdit)
private
{ Private declarations }
FHeadNum : String;
procedure keypress(var key: char);override;
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
constructor Create(AOwner:TComponent);override;
property HeadNum : String read FHeadNum write FHeadNum;
end;

procedure Register;
implementation
procedure Register;
begin
RegisterComponents('fei', [TBodyCard]);
end;

constructor TBodyCard.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
Text := FHeadNum;
//我在这里处理值的显示
end;

procedure TBodyCard.keypress(var key: char);
begin
inherited KeyPress(key);
if not(key in ['0'..'9','x','X',#8])then
key := #0;
end;

end.
 
//你的 write 关键字指向了 FHeadNum 这里使得 HeadNum 属性没有出现在属性编辑器里面
//应该是 property HeadNum :String read FHeadNum write SetHeadNum;
type
TBodyCard = class(TEdit)
private
FHeadNum: String;
procedure SetHeadNum(const Value: String);
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
published
{ Published declarations }
property HeadNum :String read FHeadNum write SetHeadNum;
end;

procedure Register;
implementation
procedure Register;
begin
RegisterComponents('fei', [TBodyCard]);
end;

{ TBodyCard }
constructor TBodyCard.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Text := FHeadNum;
//我在这里处理值的显示
end;

procedure TBodyCard.SetHeadNum(const Value: String);
begin
FHeadNum := Value;
end;

end.
 
type
TBodyCard = class(TEdit)
private
FHeadNum: String;
procedure SetHeadNum(const Value: String);
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
published
{ Published declarations }
property HeadNum :String read FHeadNum write SetHeadNum;
end;

procedure Register;
implementation
procedure Register;
begin
RegisterComponents('fei', [TBodyCard]);
end;

{ TBodyCard }
constructor TBodyCard.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Text := FHeadNum;
//我在这里处理值的显示
end;

procedure TBodyCard.SetHeadNum(const Value: String);
begin
FHeadNum := Value;
Text := FHeadNum;
end;

end.
 
谢谢,我试试就给分,这年头delphi程序员越来越少,可我还是独忠于他。
 
试验成功,完全正确,辛苦两位。
 
写属性字段一般都用方法
 
有没有搞错,属性的读用函数,写一般都是用方法的啊~~~~~~~~~~~
 
多人接受答案了。
 

Similar threads

后退
顶部