比如onmouseup吧,它会传过来一个sender对象,我把窗体上所有控件都用了一个函数,每个控件被点击后都会记录下被点击的控件,我用的是指针方法,temp^:=sender;可是当这个事件函数结束后,sender被释放了,所以我在这个函数外用temp时,就出现了溢出.怎么解决这个问题呢。

解决方案 »

  1.   

    如果一定要在外边调用的话,你可以使用外边定义的数组,链表等来保存它。当然是保存指针。如果只是在原函数内使用的话,可以考虑使用static型变量,即静态变量,函数结束后他也照样保存着变量内的内容。
      

  2.   

    楼上的兄弟,你应该知道SENDER是什么吧?它是控件!!!我不想再自己开发一个新的控件,我只想用原来的VCL,但我还需要它的SENDERf
      

  3.   

    temp : tcontrol;temp :=sender;
      

  4.   

    nam: string;nam := Sender.name;//use T***(FindComponent(nam))
      

  5.   

    ok
    temp定义为外部变量
    temp:=TComcopment;
    事件了
    temp:=Sender;
      

  6.   

    TO: rockswj(石头)
    temp定义为外部变量
    temp:=TComcopment;
    请问你上面这句是定义变量 ??类型是什么啊?能说具体点吗?
      

  7.   

    to lijinghe1(副乡长) :你这样的赋值可以吗?类型不对吧。
      

  8.   

    函数值的传递是复制一份,如果想引用要加上VAR,而对象的赋值一般则是引用,如果想复制一份出来则用COPY函数
      

  9.   

    不好意思,上次写的有点仓促,发出去了也没再看一下
    var
      Form1: TForm1;
      temp:TObject;
    implementation
    {$R *.DFM}
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      temp:=Sender;
    end;
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      if temp=nil then
      ShowMessage('curr temp is nil') else
      if Temp is TButton then
      ShowMessage('curr temp is Button:'+(temp As TButton).Name) else
      if Temp is TForm then
      ShowMessage('curr temp is Form:'+(temp As TForm).Name);
    end;
    procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      temp:=Sender;
    end;