Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Use of "Sender"

Okay, this may seem ultra basic, but I am wondering if there is an explicit explanation of how the Sender object can be used. It seems clear that when an event is fired, the Sender is the object at which the event occured (button, for example). But I have also seen code in which Sender is used in a Case statement to decide what kind of object sent the message in question. Are there other uses of Sender? I have Mario Cantu's Mastering Delphi 6 and Delphi in a Nutshell, but neither of them deal with something so elementary. I am looking for some kind of syntax reference for this keyword. Thanks in advance.

Paul Courtney

Comments

  • zibadianzibadian Member Posts: 6,349
    : Okay, this may seem ultra basic, but I am wondering if there is an explicit explanation of how the Sender object can be used. It seems clear that when an event is fired, the Sender is the object at which the event occured (button, for example). But I have also seen code in which Sender is used in a Case statement to decide what kind of object sent the message in question. Are there other uses of Sender? I have Mario Cantu's Mastering Delphi 6 and Delphi in a Nutshell, but neither of them deal with something so elementary. I am looking for some kind of syntax reference for this keyword. Thanks in advance.
    :
    : Paul Courtney
    :
    To my knowledge Sender can only be used to find out which object sent the event. It you have several objects sharing the same event, you can determine which object was firing the event. Here is a small code to demonstrate it:
    [code]
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    with Sender as TButton do
    ShowMessage(Name+' was clicked!');
    end;
    [/code]
    For this demo to work, you'll need a form with 2 or more buttons. All the OnClick events neet to be set to Button1Click. If you press a button, the program should show a dialog, with the name of the button you pressed.
  • pkcourtneypkcourtney Member Posts: 4
    : : Okay, this may seem ultra basic, but I am wondering if there is an explicit explanation of how the Sender object can be used. It seems clear that when an event is fired, the Sender is the object at which the event occured (button, for example). But I have also seen code in which Sender is used in a Case statement to decide what kind of object sent the message in question. Are there other uses of Sender? I have Mario Cantu's Mastering Delphi 6 and Delphi in a Nutshell, but neither of them deal with something so elementary. I am looking for some kind of syntax reference for this keyword. Thanks in advance.
    : :
    : : Paul Courtney
    : :
    : To my knowledge Sender can only be used to find out which object sent the event. It you have several objects sharing the same event, you can determine which object was firing the event. Here is a small code to demonstrate it:
    : [code]
    : procedure TForm1.Button1Click(Sender: TObject);
    : begin
    : with Sender as TButton do
    : ShowMessage(Name+' was clicked!');
    : end;
    : [/code]
    : For this demo to work, you'll need a form with 2 or more buttons. All the OnClick events neet to be set to Button1Click. If you press a button, the program should show a dialog, with the name of the button you pressed.
    :
    Actually, I have been using the Sender as a way to find out the name of the control that is being double clicked using it as follows:

    :[code]
    :procedure TfrmEditRecord.edCntrlDblClick(Sender: TObject);
    :var vCntrlName, vCntrlValue, vUserName: string;
    : vMessage, ListItem, vListBoxType, vCntrlName_ed: string;
    : vListBoxYN: boolean;
    :begin
    : vCntrlName := TComponent(Sender).Name;
    :....more code
    :end;
    :[/code]

    I found this particular usage through an example of something else entirely in the Delphi Help. It works, so that's good. But I have not found any documentation of this. Thanks in advance.

    Paul K. Courtney
Sign In or Register to comment.