Difference between revisions of "TButton/fr"

From Lazarus wiki
Jump to navigationJump to search
(Created page with "{{TButton}} A '''TButton''' image:tbutton.png is a component that provides a basic push button control. It ia available on the Standard tab of the Component Palette...")
 
m (Fixed syntax highlighting)
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
{{TButton}}
 
{{TButton}}
  
A '''TButton''' [[image:tbutton.png]] is a component that provides a basic push button control. It ia available on the [[Standard tab]] of the [[Component Palette]].
+
Un '''TButton''' [[image:tbutton.png]] est un composant qui fournit un bouton à pousser de base. Il est disponible dans l'[[Standard tab/fr|onglet Standard]] de la [[Component Palette/fr|palette de composants]].
  
A TButton is one of the most basic controls on a [[TForm|Form]]. Clicking with the mouse on it (or change with the [tab] key on the button and pressed it with enter), an action is triggered. This click is called an event. For this you need [[Event_order|event handler]] that are called after the jump.
+
Un TButton est une des contrôles les plus basiques sur une [[TForm/fr|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 [[Event_order/fr|gestionnaire d'événement]] qui est appelé après le clic.
  
You can add a button to your form, by clicking the TButton (square button with an "OK" in the middle) on the Standard component palette and place it with a click on your form.
+
Vous pouvez ajouter un bouton sur votre fiche, en cliquant sur le bouton [[image:tbutton.png]] de la palette de composant Standard et le placer sur la fiche en cliquant dessus.
  
The event handler for a mouse click can be quite easily reached in which a double-click on the pasted Button (or in the Object Inspector, select the event OnClick of your Button). The event handler for a ''Button1'' on a form ''Form1'' will look like this:
+
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 :
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
begin
 
begin
Line 14: Line 15:
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
Between the statements '''begin''' and '''end''' you could write code that is called when '''Button1''' is clicked.
 
  
Almost all available beginner tutorials use TButtons as an easy entry into the [[Glossary#OO|Object Oriented Programming]] with Lazarus. Following tutorials are well suited for beginners to understand the use of buttons:
+
Entre les instructions '''begin''' et '''end''', vous pourrez écrire du code qui est appelé quand ''Button1'' est cliqué.
* [[Form_Tutorial#The_first_GUI_application|The first GUI application]] for absolute beginners
+
 
* [[Lazarus_Tutorial#Your_first_Lazarus_Program.21|Your first Lazarus program]] tutorial for Lazarus
+
Presque tous les tutoriels pour débutants utilisent les TButton comme un façon simple d'entrer dans la [[Glossary/fr#OO|Programmation Orientée Objet]] avec Lazarus. Les tutoriels suivants sont bien adaptés aux débutants pour comprendre l'utilisation des boutons :
* [[Object_Oriented_Programming_with_FreePascal_and_Lazarus#Programming_Example|Programming Example]] Object Oriented Programming with Free Pascal and Lazarus
+
* [[Form_Tutorial/fr#The_first_GUI_application|La première application IHM]] pour les débutants qui débutent vraiment.
 +
* [[Lazarus_Tutorial/fr#Votre_premier_programme_sous_Lazarus_.21|Votre premier programme Lazarus]], tutoriel pour Lazarus.
 +
* [[Object_Oriented_Programming_with_Free_Pascal_and_Lazarus/fr#Exemple_de_programmation|Exemple de programmation]] Programmation Orientée Objet avec Free Pascal et Lazarus.
  
 
= Un simple exemple =
 
= Un simple exemple =
* Create a new application and drop a TButton on the form.
+
* Créez une nouvelle application et posez un bouton sur la fiche.
* Doubleclick this ''Button1'' on the form (the default handler: ''OnClick'' is created for ''Button1'', the source text editor opens).
+
* Double-cliquez sur ce ''Button1'' (le gestionnaire par défaut : ''OnClick'' est créé pour le ''Button1'' et l'éditeur de source s'ouvre).
* Add following code in the event handler:
+
* Ajoutez le code suivant dans le gestionnaire d'événement :
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
begin
 
begin
Line 31: Line 34:
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
* Start your program (with Key <F9>).
+
 
 +
* Démarrez votre programme (avec la touche {{keypress|F9}}).  
  
 
= Clic-droit =
 
= Clic-droit =
Each TButton has an (optional) PopupMenu property that will activate a connected [[TPopupMenu]] whenever the button is right-clicked.
+
Chaque TButton a une propriété PopupMenu (optionnelle) qui activera un [[TPopupMenu/fr|TPopupMenu]] qui lui est connecté à chaque fois que le bouton est cliqué-droit.
  
= Boutons générés dynmaiquement =
+
= Boutons générés dynamiquement =
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.
+
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.
  
This can be achieved as in the the following example (a quick calculator):
+
Cela peut être réalisé comme dans l'exemples qui suit (une calculatrice vite fait) :
* Create a new '''blank''' [[Form_Tutorial#The_first_GUI_application|GUI application]] with the form ''Form1'' and add '''StdCtrls''' to the [[Uses|uses clause]] (here the TButton is).
+
* Créez une nouvelle [[Form_Tutorial#The_first_GUI_application|application IHM]] '''blanche''' avec la fiche ''Form1'' et ajoutez '''StdCtrls''' à la [[Uses/fr|uses clause]] (où le TButton se trouve).
* Change caption ''Form1'' to ''QuickAdd''.
+
* Modifiez l'intitulé (''Caption'') ''Form1'' en ''QuickAdd''.
* Create the OnCreate event handler of Form1 (go in the Object Inspector to the event ''OnCreate'' and click the button [...]).
+
* Créez le gestionnaire d'événement OnCreate de la fiche Form1 (cliquez sur le bouton [...] de l'événement OnCreate de l'inspecteur d'objet).
* Add following code:
+
* Ajoutez le code suivant :
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
procedure TForm1.FormCreate(Sender: TObject);
 
procedure TForm1.FormCreate(Sender: TObject);
 
var
 
var
Line 62: Line 67:
 
end;  
 
end;  
 
</syntaxhighlight>
 
</syntaxhighlight>
* Now you must create the event handler for the button clicks.
+
 
* In the source editor, entering your class ''TForm1'' in the section '''<code>private</code>'''.
+
* Maintenant vous devez créer le gestionniaire d'événement pour les clics de bouton.
* Add '''<code>procedure aButtonClick(Sender: TObject);</code>''' and then press the keys [CTRL] + [Shift] + [c] (the code completion becomes active and creates the procedure <code>TForm1.aButtonClick(Sender: TObject);</code>.
+
* Dans l'éditeur de source, allez dans la section '''<code>private</code>''' de votre classe ''TForm1''.
* Paste following code:
+
* Ajoutez '''<code>procedure aButtonClick(Sender: TObject);</code>''' et ensuite pressez sur les touches {{keypress|Ctrl}} + {{keypress|Shift}} + {{keypress|c}} (la complétion de code devient active et crée le corps de la procédure <code>TForm1.aButtonClick(Sender: TObject);</code>.
<syntaxhighlight>
+
* Coller le code suivant :
 +
 
 +
<syntaxhighlight lang=pascal>
 
procedure TForm1.aButtonClick(Sender: TObject);
 
procedure TForm1.aButtonClick(Sender: TObject);
 
const
 
const
Line 81: Line 88:
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
* Start your application.
+
 
{{Note|You can assign every imaginable event handlers to your buttons, as long as this the form '''<code>procedure <class>.<name of procedure>(Sender: TObject);</code>''' has. Thus, you can use one from another class!}}
+
* Démarrez votre application ({{keypress|F9}}).
 +
{{Note|Vous pouvez affecter tous les gestionnaires d'événement imaginables à vos bouton, pourvu qu'ils soient de la forme '''<code>procedure <class>.<name of procedure>(Sender: TObject);</code>'''. Vous pouvez aussi en utiliser un qui vient d'une autre classe !}}
  
 
= Voir aussi =
 
= Voir aussi =
 +
 
* [[doc:lcl/stdctrls/tbutton.html|Doc. TButton]]
 
* [[doc:lcl/stdctrls/tbutton.html|Doc. TButton]]
 
* [[TBitBtn/fr|TBitBtn]]
 
* [[TBitBtn/fr|TBitBtn]]

Latest revision as of 05:52, 29 February 2020

Deutsch (de) English (en) español (es) suomi (fi) français (fr) 日本語 (ja) русский (ru)

Un TButton tbutton.png 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 tbutton.png 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 :

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

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) :

  • Créez une nouvelle application IHM blanche avec la fiche Form1 et ajoutez StdCtrls à la uses clause (où le TButton se trouve).
  • Modifiez l'intitulé (Caption) Form1 en QuickAdd.
  • Créez le gestionnaire d'événement OnCreate de la fiche Form1 (cliquez sur le bouton [...] de l'événement OnCreate de l'inspecteur d'objet).
  • Ajoutez le code suivant :
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;
  • Maintenant vous devez créer le gestionniaire d'événement pour les clics de bouton.
  • Dans l'éditeur de source, allez dans la section private de votre classe TForm1.
  • Ajoutez procedure aButtonClick(Sender: TObject); et ensuite pressez sur les touches Ctrl + Shift + c (la complétion de code devient active et crée le corps de la procédure TForm1.aButtonClick(Sender: TObject);.
  • Coller le code suivant :
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;
  • Démarrez votre application (F9).
Light bulb  Remarque: 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


Composant LCL
Onglet de palette Composants
Standard TMainMenu • TPopupMenu • TButton • TLabel • TEdit • TMemo • TToggleBox • TCheckBox • TRadioButton • TListBox • TComboBox • TScrollBar • TGroupBox • TRadioGroup • TCheckGroup • TPanel • TFrame • TActionList
Additional TBitBtn • TSpeedButton • TStaticText • TImage • TShape • TBevel • TPaintBox • TNotebook • TLabeledEdit • TSplitter • TTrayIcon • TControlBar • TFlowPanel • TMaskEdit • TCheckListBox • TScrollBox • TApplicationProperties • TStringGrid • TDrawGrid • TPairSplitter • TColorBox • TColorListBox • TValueListEditor
Common Controls TTrackBar • TProgressBar • TTreeView • TListView • TStatusBar • TToolBar • TCoolBar • TUpDown • TPageControl • TTabControl • THeaderControl • TImageList • TPopupNotifier • TDateTimePicker
Dialogs TOpenDialog • TSaveDialog • TSelectDirectoryDialog • TColorDialog • TFontDialog • TFindDialog • TReplaceDialog • TOpenPictureDialog • TSavePictureDialog • TCalendarDialog • TCalculatorDialog • TPrinterSetupDialog • TPrintDialog • TPageSetupDialog • TTaskDialog
Data Controls TDBNavigator • TDBText • TDBEdit • TDBMemo • TDBImage • TDBListBox • TDBLookupListBox • TDBComboBox • TDBLookupComboBox • TDBCheckBox • TDBRadioGroup • TDBCalendar • TDBGroupBox • TDBGrid • TDBDateTimePicker
Data Access TDataSource • TBufDataset • TMemDataset • TSdfDataSet • TFixedFormatDataSet • TDbf
System TTimer • TIdleTimer • TLazComponentQueue • THTMLHelpDatabase • THTMLBrowserHelpViewer • TAsyncProcess • TProcessUTF8 • TProcess • TSimpleIPCClient • TSimpleIPCServer • TXMLConfig • TEventLog • TServiceManager • TCHMHelpDatabase • TLHelpConnector
Misc TColorButton • TSpinEdit • TFloatSpinEdit • TArrow • TCalendar • TEditButton • TFileNameEdit • TDirectoryEdit • TDateEdit • TTimeEdit • TCalcEdit • TFileListBox • TFilterComboBox • TComboBoxEx • TCheckComboBox • TButtonPanel • TShellTreeView • TShellListView • TXMLPropStorage • TINIPropStorage • TIDEDialogLayoutStorage • TMRUManager • TStrHolder
LazControls TCheckBoxThemed • TDividerBevel • TExtendedNotebook • TListFilterEdit • TListViewFilterEdit • TTreeFilterEdit • TShortPathEdit • TLvlGraphControl
RTTI TTIEdit • TTIComboBox • TTIButton • TTICheckBox • TTILabel • TTIGroupBox • TTIRadioGroup • TTICheckGroup • TTICheckListBox • TTIListBox • TTIMemo • TTICalendar • TTIImage • TTIFloatSpinEdit • TTISpinEdit • TTITrackBar • TTIProgressBar • TTIMaskEdit • TTIColorButton • TMultiPropertyLink • TTIPropertyGrid • TTIGrid
SQLdb TSQLQuery • TSQLTransaction • TSQLScript • TSQLConnector • TMSSQLConnection • TSybaseConnection • TPQConnection • TPQTEventMonitor • TOracleConnection • TODBCConnection • TMySQL40Connection • TMySQL41Connection • TMySQL50Connection • TMySQL51Connection • TMySQL55Connection • TMySQL56Connection • TSQLite3Connection • TIBConnection • TFBAdmin • TFBEventMonitor • TSQLDBLibraryLoader
Pascal Script TPSScript • TPSScriptDebugger • TPSDllPlugin • TPSImport_Classes • TPSImport_DateUtils • TPSImport_ComObj • TPSImport_DB • TPSImport_Forms • TPSImport_Controls • TPSImport_StdCtrls • TPSCustomPlugin
SynEdit TSynEdit • TSynCompletion • TSynAutoComplete • TSynMacroRecorder • TSynExporterHTML • TSynPluginSyncroEdit • TSynPasSyn • TSynFreePascalSyn • TSynCppSyn • TSynJavaSyn • TSynPerlSyn • TSynHTMLSyn • TSynXMLSyn • TSynLFMSyn • TSynDiffSyn • TSynUNIXShellScriptSyn • TSynCssSyn • TSynPHPSyn • TSynTeXSyn • TSynSQLSyn • TSynPythonSyn • TSynVBSyn • TSynAnySyn • TSynMultiSyn • TSynBatSyn • TSynIniSyn • TSynPoSyn
Chart TChart • TListChartSource • TRandomChartSource • TUserDefinedChartSource • TCalculatedChartSource • TDbChartSource • TChartToolset • TChartAxisTransformations • TChartStyles • TChartLegendPanel • TChartNavScrollBar • TChartNavPanel • TIntervalChartSource • TDateTimeIntervalChartSource • TChartListBox • TChartExtentLink • TChartImageList
IPro TIpFileDataProvider • TIpHttpDataProvider • TIpHtmlPanel