TButton/fr
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
日本語 (ja) │
русский (ru) │
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 :
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 :
- La première application IHM pour les débutants qui débutent vraiment.
- Votre premier programme Lazarus, tutoriel pour Lazarus.
- Exemple de programmation Programmation Orientée Objet avec Free Pascal et Lazarus.
Un simple exemple
- Créez une nouvelle application et posez un bouton sur la fiche.
- Double-cliquez sur ce Button1 (le gestionnaire par défaut : OnClick est créé pour le Button1 et l'éditeur de source s'ouvre).
- Ajoutez le code suivant dans le gestionnaire d'événement :
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Lazarus makes my day'); //A message will be displayed with the content...
end;
- Démarrez votre programme (avec la touche F9).
Clic-droit
Each TButton has an (optional) PopupMenu property that will activate a connected TPopupMenu whenever the button is right-clicked.
Boutons générés dynmaiquement
Sometimes, instead of creating buttons (or other components) with the Lazarus form designer, it is easier to create them dynamically at run time. This approach is useful especially if you have continually repeated buttons on a form.
This can be achieved as in the the following example (a quick calculator):
- Create a new blank GUI application with the form Form1 and add StdCtrls to the uses clause (here the TButton is).
- Change caption Form1 to QuickAdd.
- Create the OnCreate event handler of Form1 (go in the Object Inspector to the event OnCreate and click the button [...]).
- Add following code:
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;
- Now you must create the event handler for the button clicks.
- In the source editor, entering your class TForm1 in the section
private
. - Add
procedure aButtonClick(Sender: TObject);
and then press the keys [CTRL] + [Shift] + [c] (the code completion becomes active and creates the procedureTForm1.aButtonClick(Sender: TObject);
. - Paste following code:
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;
- Start your application.
procedure <class>.<name of procedure>(Sender: TObject);
has. Thus, you can use one from another class!Voir aussi