继承TPanel写个类TPanel1,在上面放上一个TEdit。  
type  
   TPanel1  =  class(TPanel)  
   private  
       FEdit:Tedit;  
   protected  
   public  
       constructor  Create(AOwner:  TComponent);override;  
       destructor  Destroy;override;  
   published  
       property  Edit:  TEdit  read  FEdit  write  FEdit;  
   end;  
.........  
constructor  TPanel1.Create(AOwner:  TComponent);  
begin  
   inherited;  
   FEdit  :=  TEdit.Create(Self);  
   FEdit.SetSubComponent(True);  
   FEdit.Height  :=  16;  
   FEdit.Width  :=  60;  
   FEdit.Parent  :=  Self;  
end;  
 
destructor  TPanel1.Destroy;  
begin  
   FEdit.Free;  
   inherited;  
end;  
 
我想在Edit的事件中写程序,可是双击Edit的事件时提示  
cannot  creat  a  method  for  an  unnamed  component.  
请问如何解决这个问题。  
或者我想自定义一个TPanel1事件(比如OnEditKeyPress)来相应Edit的这一事件(OnKeyPress),又该如何写。

解决方案 »

  1.   

    因为FEdit没有命名啊,双击时,IDE一般自动生成:组件名+‘Click’的事件句柄,如‘Button2Click’,所以可以在设计时编辑的组件应该命名。可以在
    FEdit  :=  TEdit.Create(Self);  
    后加上:
    FEdit := 'PanelEdit1';  //或者其他名字另:
    property  Edit:  TEdit  read  FEdit  write  FEdit;  
    应该为
    property  Edit:  TEdit  read  FEdit;因为Edit是内部创建的,在外部不应该能修改。
    ————————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    ————————————————————————————————————
      

  2.   

    哇,高手来解答咯,呵呵,《Delphi 精要》的作者哦!
      

  3.   

    支持 lxpbuaa(桂枝香在故国晚秋)
      

  4.   

    原来如此,谢谢!请问第二个问题
    如果自定义一个TPanel1事件(比如OnEditKeyPress)来相应Edit的这一事件(OnKeyPress),该如何解决。
      

  5.   

    juliens(星星球) :过奖过奖
     CDSoftwareWj(95927) :最近工作太忙:)private
         FEdit: TEdit;
         procedure SetOnEditKeyPress(Value: TKeyPressEvent);
         function GetOnEditKeyPress: TKeyPressEvent;
    published
         property  Edit:  TEdit  read  FEdit;
         property OnEditKeyPress: TKeyPressEvent read GetOnEditKeyPress write SetOnEditKeyPress;
       end;
    procedure TPanel1.SetOnEditKeyPress(Value: TKeyPressEvent);
    begin
      if FEdit <> nil then
        FEdit.OnKeyPress := Value;
    end;function TPanel1.GetOnEditKeyPress: TKeyPressEvent;
    begin
      Result := nil;
      if FEdit <> nil then
        Result := FEdit.OnKeyPress;
    end;————————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    ————————————————————————————————————