Project1.dpr

program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.



Unit1.pas

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
BitBtn1: TBitBtn;
Label1: TLabel;
Label2: TLabel;
Memo1: TMemo;
Label3: TLabel;
Label4: TLabel;
procedure ButtonClick(Sender: TObject);
private
{ Déclarations privées }
public
{ Déclarations publiques }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ButtonClick(Sender: TObject);
begin
if Sender is TButton then
begin
Label1.Caption:='vous avez cliqué sur '+TButton(Sender).Name+
' dont le libellé est '+TButton(Sender).Caption;
// vous pouvez donc trouver quel bouton a été cliqué. => vous pouvez traitement le bouton approprié.
end;


//autre solution : utilisation de as
if Sender is TForm then
begin
With Sender as TForm do
begin
Label1.Caption:='vous avez cliqué sur '+Name+
' dont le libellé est '+Caption;
end;
end;


// au besoin, on peut remonter à un type ancètre.
// Les Buttons, Forms et Memos ont par exemple un ancètre commun : TControl
// Ceci va donc se déclencher lorsque l'on va cliquer sur les boutons
// mais aussi lorsque l'on va cliquer sur la Form1 et sur Memo1.
if Sender is TControl then
begin
Label4.Caption:='vous avez cliqué sur '+TControl(Sender).Name;
end

end;

end.

 

 

 


Reply