写了一个可以自动保存窗口位置状态的组件,本来觉得很简单,现在却碰到了点小问题,调试不过去,麻烦大家帮我看看。// 为了在装入组件的时设置窗口位置,重载了TComponent的loaded方法
procedure TFormState.Loaded;
begin
   inherited Loaded;
   if not (csDesigning in Componentstate) then begin
      ReadFormPos(Owner as TForm);  // 读取上次保存的窗口参数
      FOnDestroy := (Owner as TForm).OnDestroy; // 一执行到此句便出错,非法访问
      (Owner as TForm).OnDestroy := FormDestroy;
   end;
end;

解决方案 »

  1.   

    单步调试时,发现 Owner 居然为 nil ????
      

  2.   

    TFormState.Owner你没设置.在TFormState.Create设置一下。
      

  3.   

    to hch_45(HCH ~ahong.net~) :我重载了Create方法,
    constructor TFormState.Create(AOwner: TComponent);
    begin
       inherited Create(AOwner);
    end;应该设置了啊
      

  4.   

    to cll007(gazo): TFormState的Owner应该创建它的窗口啊
      

  5.   

    constructor TFormState.Create(AOwner: TComponent);
    begin
       inherited Create(AOwner);
       Owner:=AOwner;
    end;
      

  6.   

    to cll007(gazo):Owner 属性是只读的
      

  7.   

    那你的Owner怎么初始化,如果不赋值,肯定是nil或其它非法数值
      

  8.   

    我记得Loaded是在Create之后发生的,所以Owner为nil。
      

  9.   

    我写错了:
    我记得Loaded是在Create之前发生的,所以Owner为nil。
      

  10.   

    To snowfog(秋风舞): 
    我细查了一下delphi的帮助文档,Loaded应该是在Create之后发生的吧?When the streaming system loads a form or data module from its form file, it first constructs the form component by calling its constructor, then reads its property values from the form file. After reading all the property values for all the components, the streaming system calls the Loaded methods of each component in the order the components were created. This gives the components a chance to initialize any data that depends on the values of other components or other parts of itself.
      

  11.   

    那FormState的Create呢?
    你总得有创建时间吧?
    FormState := TFromState.Create(nil)?
    那就是nil了
    Create(self)呢?》
    Application.Create(TF,F);
      

  12.   

    俺刚刚开始学习Delphi,向各位前辈学习!接分!顶!
      

  13.   

    to ttzzj(标识符) :TFormState是个设计期组件,目的想哪给窗口需要保存窗口状态,就在哪个窗口放一个。
    这样的话,不是该组件被拖放到窗口就会执行Create了吗?在运行期,create也同样会执行的。