我自己写了一个控件,继承自 TCustomEdit
TMyEdit=class(TCustomEdit)
  public
    Edit:Tedit;
  end;
在程序中我定义了一个全局变量: var MyEdit:TMyedit;
在Button1click(send:Tobjedt)中有如下代码:
begin
  Myedit:=TMyedit.create(self);
  Myedit.parent:=self;
  Myedit.edit:=edit3;   //edit3 是窗体上的Tedit;
  //注意,下面的这行代码执行正确
  Myedit.edit.text:='Hello';
end;
在Button2click(send:tobject) 中代码是这样的:
begin
  //执行到这行的时候 Myedit.edit 居然是 Nil  !!!!  实在不解
  if assigned(Myedit.edit) then myedit.edit.text:='Changed';
end;
运行时我选单击 Button1 ,一切正常,控件正确创建,并且Edit3的几容也改变了。
但当我再单击 Button2 时, assigned(myedit.edit) 居然是 False, 也就是说
 Myedit.edit=Nil  我实在是百思不得其解!
问题就在上面,不知大家可有遇到过?
不知这是否Delphi 6 的 Bug
***********************************************************************
实在没办法了,我重写了一遍控件,可这回又出来新的玩艺了!
我把源程序贴上,大家帮我看一下吧!
如果能留下Email,我就把整个程序发过去。*******************************************************************************
控件程序如下:
unit DataEdits;interfaceuses
  Windows, buttons,Messages, SysUtils, Classes, Controls, StdCtrls;const
  tBoxIsString=0;
  tBoxIsInt=1;
  tBoxIsHasDec=2;
  tBoxIsIntStr=3;  tBoxIsMoney=5;
type
  TUserDefBtn=class;  TDataEdit = class(TCustomEdit)
  private
    Fbutton:TUserDefBtn;
    FHasButton:boolean;
    FStrLen:integer;
    FIntNum:Integer;
    FDecNum:integer;
    procedure MoveButton;
    procedure WmGetFocus(var msg:Tmessage);message wm_setFocus;
    procedure WMKillFocus(var msg:Tmessage);message WM_KILLFOCUS;
    procedure SetHasButton(hasbutton:boolean);
    { Private declarations }
  protected
    { Protected declarations }
  public
    edit:tedit;
    procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
    procedure InButtonClick(send:Tobject);
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property HasButton:boolean read fhasbutton write sethasbutton;
    { Public declarations }
  published
    { Published declarations }
  end;  TUserDefBtn=class(Tspeedbutton)
  private  protected  public
    Edit:TDataEdit;
    procedure click;override;
    constructor Create(AOwner: TComponent); override;
  end;procedure Register;implementation
//确定是否有按钮
procedure Tdataedit.SetHasButton(hasbutton:boolean);
begin
  if hasbutton=fhasbutton then exit;
  fhasbutton:=hasbutton;
  if Focused and fhasbutton then fbutton.Visible:=true;
end;
//失去焦点
procedure TDataEdit.WMKillFocus(var msg:Tmessage);
begin
  fbutton.Visible:=false;
  inherited;
end;
//得到焦点
procedure TDataEdit.WmGetFocus(var msg:tmessage);
begin
  if fhasbutton then
    fbutton.Visible:=true;
  inherited;
end;//有问题的代码段就在这里了!
//运行能通过,不过结果可就不敢恭维了,绝对会让你大跌眼镜
//愿意是让本身的文本框内容等于某个值,可却让窗口的标题栏变掉了,自己还没变
procedure Tdataedit.inbuttonclick(send:Tobject);
begin
  self.Text:='Self text Changed Succeed...';
//  if self.edit<>nil then
//    self.edit.Text:='Succeed...';
  //if assigned(edit) then
  //  edit.Text:='Succeed...';
end;
//定义按钮的单击事件
procedure TUserDefBtn.click;
begin
  Tdataedit(owner).InButtonClick(self);
end;
//文本框移动时设置按钮位置
procedure tdataedit.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
  inherited setbounds(aleft,atop,awidth,aheight);
  movebutton;
end;
//文本框移动时记按钮跟着移动
procedure tdataedit.MoveButton;
begin
  if assigned(fbutton) then
  begin
    fbutton.Top:=top;
    fbutton.Left:=left+width+2;
    fbutton.height:=height;
    fbutton.Width:=height;
  end;
end;
//建立控件
constructor TDataEdit.Create(AOwner:Tcomponent);
begin
  inherited create(aowner);
  text:='';
  //Parent:=twincontrol(aowner);
  fbutton:=TUserDefBtn.Create(aowner);
  fbutton.Visible:=false;
  fhasbutton:=true;
  fbutton.Caption:='...';
  movebutton;
end;
//创建编辑框随带的快捷按钮
constructor tuserdefbtn.create(aowner:tcomponent);
var
  Hour,Min,Sec,Msec:Word;
begin
  inherited create(aowner);
  parent:=twincontrol(aowner);
  Edit:=tDataEdit(aowner);
//给按钮控件随便安一个名字,不然运行时会有错误提示没名字
  decodetime(now,hour,min,sec,Msec);
  if Name='' then Name:='uTmpBtn'+inttostr(Msec)+inttostr(sec)+inttostr(min);
end;destructor tdataedit.Destroy;
begin
  if assigned(fbutton) then fbutton.Free;
  inherited;
end;procedure Register;
begin
  RegisterComponents('Plus', [TDataEdit]);
end;
end.*******************************************************************************
主程序如下:
unit Unit1;interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,dataedits,
  Dialogs, StdCtrls, ImgList, DB, ADODB, Grids, DBGrids, Buttons;type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  ExmEdit:Tdataedit;implementation//uses tkmtreeviews;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
  exmedit:=tdataedit.Create(self);
  exmedit.top:=100;
  exmedit.left:=124;
  exmedit.Parent:=self;
  exmedit.edit:=edit1;
  exmedit.edit.Text:='Create Component';
  exmedit.SetFocus;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
  if assigned(exmedit) then
  begin
    exmedit.Text:='Button Change';
    exmedit.edit.Text:='Setting Change...';
    exmedit.SetFocus;
  end;
end;end.
******************************************************************************
大家可以运行一下看。这个问题已经把我搞得半死了!如留下Email 我会发一份完整的程序过去。希望各路高手多多帮忙了!