Sign in Join
Wiki copytemporary

TButton/fr

Deutsch · English · Español · Français · Suomi · Русский · 日本語

Un TButton est un composant qui fournit un bouton à pousser de base. Il est disponible dans l'onglet Standard de la palette de composants.

Un TButton est une des contrôles les plus basiques sur une fiche. En cliquant dessus avec la souris ou en se plaçant dessus à l'aide de la touche [tab] et en actionnant ensuite [Entrée], une action est déclenchée. Ce clic est appelé un événement. Pour réaliser l'action, vous avez besoin d'un gestionnaire d'événement qui est appelé après le clic.

Vous pouvez ajouter un bouton sur votre fiche, en cliquant sur le bouton de la palette de composant Standard et le placer sur la fiche en cliquant dessus.

Le gestionnaire d'événement pour le clic souris peut être atteint facilement en double-cliquant sur le botoun posé (ou en passant par l'inspecteur d'objet, onglet Evénements). Le gestionnaire d'événement pour le Button1 de la Form1 ressemblera à cela :

pascal
procedure TForm1.Button1Click(Sender: TObject);
begin

end;

Entre les instructions begin et end, vous pourrez écrire du code qui est appelé quand Button1 est cliqué.

Presque tous les tutoriels pour débutants utilisent les TButton comme un façon simple d'entrer dans la Programmation Orientée Objet avec Lazarus. Les tutoriels suivants sont bien adaptés aux débutants pour comprendre l'utilisation des boutons :

Un simple exemple

pascal
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('Lazarus makes my day');  //A message will be displayed with the content...
end;

Clic-droit

Chaque TButton a une propriété PopupMenu (optionnelle) qui activera un TPopupMenu qui lui est connecté à chaque fois que le bouton est cliqué-droit.

Boutons générés dynamiquement

Parfois, au lieu de créer les boutons (ou d'autres composants) avec l'éditeur de fiche de Lazarus, il est plus facile de créer les composants dynamiquement à l'exécution, cette approche est utile en particulier si vous avez des boutons continuellement répétés sur une fiche.

Cela peut être réalisé comme dans l'exemples qui suit (une calculatrice vite fait) :

pascal
procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  aButton: TButton;
begin
  for i:=0 to 9 do begin              //create 10 Buttons 
    aButton:=TButton.Create(Self);    //create Button, Owner is Form1, where the button is released later
    aButton.Parent:=Self;             //determine where it is to be displayed
    aButton.Width:=aButton.Height;    //Width should correspond to the height of the buttons
    aButton.Left:=i*aButton.Width;    //Distance from left
    aButton.Caption:=IntToStr(i);     //Captions of the buttons (0.9)
    aButton.OnClick:=@aButtonClick;   //the event handler for the button -> will be created yet
  end;
  Self.Height:=aButton.Height;        //Height of the form should correspond to the height of the buttons
  Self.Width:=aButton.Width*10;       //Width of the form to match the width of all buttons
end;
pascal
procedure TForm1.aButtonClick(Sender: TObject);
const
  Cnt: Integer = 0;
var
  i: Integer;
begin
  if (Sender is TButton) and                       //called the event handler of a button out?
     TryStrToInt(TButton(Sender).Caption, i)       //then try to convert the label in a integer
  then begin
    Cnt:=Cnt + i;                                 //the adding counter is incremented by the number of entrechende
    Caption:='QuickAdd: '+IntToStr(Cnt);          //write the result to the caption of the form
  end;
end;

Note: Vous pouvez affecter tous les gestionnaires d'événement imaginables à vos bouton, pourvu qu'ils soient de la forme procedure <class>.<name of procedure>(Sender: TObject);. Vous pouvez aussi en utiliser un qui vient d'une autre classe !

Voir aussi


From the Free Pascal wiki, page "TButton/fr", as it stood on 2020-02-29 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.