Sender,self代表什么,什么时候用

解决方案 »

  1.   

    self是指你具体操作控件的本身,例如TEdit.creat(self);
    Sender可以指任何一个控件。
      

  2.   

    Self指代你当前定义的函数或方法归属的类的实例
     Sender指代当前引发的事件的控件本身。
      

  3.   

    self是指你具体操作控件的本身,例如TEdit.creat(self);
    在一个单元里用时,即表示本单元的窗体(控件)本身,在单元里用self.时,本窗体的所有控件名,属性等都会列出来。
    sender是指你操作的控件
    如在一个控件的双击事件中TfrmMain.Button1Click(Sender: TObject);
    sender 就是Button1
      

  4.   

    self就不说了,看看sender吧,下面是delphi帮助里面的,关于sender参数的
    *****************************************************
    In an event handler, the Sender parameter indicates which component received the event and therefore called the handler. Sometimes it is useful to have several components share an event handler that behaves differently depending on which component calls it. You can do this by using the Sender parameter in an if...then...else statement. For example, the following code displays the title of the application in the caption of a dialog box only if the OnClick event was received by Button1.procedure TMainForm.Button1Click(Sender: TObject);
    begin
    if Sender = Button1 then
      AboutBox.Caption := 'About ' + Application.Title
    else 
      AboutBox.Caption := '';
    AboutBox.ShowModal;
    end;
    ********************************************************************
    我想这些E文你能看得懂吧。
      

  5.   

    Sender是明白了,self是否代表的就是当前的宿主控件呢
      

  6.   

    Sender,Self,Owner,parent-----------------------------------------------------Sender--意义:指本对象。Sender在什么对象相关代码里,那么Sender就是什么。Self--意义:指本类,也就是Self被引用的类。比如若在类TMyClass内引用了Self,那么Self=TMyClass.Owner--意义:哪个对象释放我的内存啊?如:Pan:=TPanel.Create(Self);其中Create的参数是:AOwner:TComponent。Owner释放Pan的内存。因为窗口释放Pan的内存,但窗口类的对象是Self.一般给Owner传Self就可以。比如:代码段一:pan:=TPanel.Create(Self);with Pan do begintryLeft:=20;Top:=20;parent:=Self; //Parent:=Form1也可以。Visible:=true;ShowMessage('Created');finallyPan.free;end;end;-----------------------------------------------------Parent--意义:此对象包括在哪个对象里那?说明:若组件不是从TControl继承来的,那么在创建组件后不必声明此属性
      

  7.   

    在李维的VCL FRAMEWORK 构架剖析一书所说的,self是在每个对象被创建时由编绎器生成的代表本对象
    ,主要由内存地址体现
      

  8.   

    Self指代你当前定义的函数或方法归属的类的实例
     Sender指代当前引发的事件的控件本身
      

  9.   

    明白了,Button1Click其实就是TForm1类的一个方法,所以Self还是指向的TForm1的实例也就是Form1
      

  10.   

    self 是你.
    sender 是他.