Difference between revisions of "TMemo/fr"

From Lazarus wiki
Jump to navigationJump to search
(Created page with "{{TMemo}} A '''TMemo''' image:tmemo.png is a control with multiple lines of editable text. It is available from the Standard tab of the Component Palette. = Usag...")
 
m (Fixed syntax highlighting)
 
(8 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
{{TMemo}}
 
{{TMemo}}
  
A '''TMemo''' [[image:tmemo.png]] is a control with multiple lines of editable text. It is available from the [[Standard tab]] of the [[Component Palette]].
+
Un '''TMemo''' [[image:tmemo.png]] est un contrôle d'édition de texte sur plusieurs lignes. Il est disponible depuis l'[[Standard tab/fr|onglet Standard]] de la  [[Component Palette/fr|palette de composants]].
  
 
= Usage =
 
= Usage =
To use a TMemo on a [[TForm|form]], you can simply select it on the ''Standard'' component pallet and place it by clicking on the form. In this text box, you can now edit a multiline text at run time.
+
Pour utiliser un TMemo sur une [[TForm/fr|fiche]], vous pouvez simplement le sélectionner dans la palette ''Standard'' et le placer en cliquant sur la fiche. Dans cette zone de saisie, vous pouvez maintenant éditer un texte multi-ligne à l'exécution.
  
For example, have you added a TMemo ''Memo1'' to your form ''Form1'', you can use '''<code>Memo1.Text:='this is a singleline text';</code>''' to assign a [[String]].
+
Par exemple, dès que vous avez ajouté un TMemo ''Memo1'' à votre fiche ''Form1'', vous pouvez utilisez '''<code>Memo1.Text:='this is a singleline text';</code>''' pourt affecter une [[String/fr|chaîne]].
  
Also you can use anywhere in the source code the stored text of ''Memo1'' with '''<code>myString:=Memo1.Text;</code>'''.
+
Vous pouvez aussi utiliser le texte partout dans le code source le texte stocké dans ''Memo1'' avec '''<code>myString := Memo1.Text;</code>'''.
  
It is also possible to assign a multiline text with '''<code>Memo1.Text:='This'+LineEnding+'is'+LineEnding+'a'+LineEnding+'multiline'+LineEnding+'text';
+
Il est aussi possible d'affecter un texte multi-ligne avec '''<code>Memo1.Text := 'This'+LineEnding+'is'+LineEnding+'a'+LineEnding+'multiline'+LineEnding+'text';</code>'''.
</code>'''.
 
  
 
== Affectation d'une TStrings ou d'une TStringList ==
 
== Affectation d'une TStrings ou d'une TStringList ==
Common, to assign a text to a TMemo is the use of a [[TStringList-TStrings Tutorial|TStringList]] or its parent [[TStringList-TStrings Tutorial|TStrings]]. The following example shows this (in the event handler of an inserted [[TButton]] ''Button1'' in a [[TForm|form]] ''Form1'' and a TMemo ''Memo1'' on it):
+
Un usage courant pour assigner un texte à un TMemo est l'utilisation d'un [[TStringList-TStrings Tutorial/fr|TStringList]] ou de son parent[[TStringList-TStrings Tutorial/fr|TStrings]]. L'exemple suivant montre ceci (dans le gestionnaire d'événement d'un [[TButton/fr|TButton]] ''Button1'' inséré dans la [[TForm/fr|fiche]] ''Form1'' avec un TMemo ''Memo1'' posé dessus).
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
var
 
var
Line 31: Line 30:
  
 
== Insérer des lignes directement ==
 
== Insérer des lignes directement ==
You can add directly the contents of the memo for example:
+
Vous pouvez ajouter directement le contenu du mémo, par exemple :
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button1Click(Sender: TObject);
 
procedure TForm1.Button1Click(Sender: TObject);
 
begin
 
begin
Line 44: Line 43:
  
 
== Lire une ligne ==
 
== Lire une ligne ==
If you want to know what is in a particular line, you can check it directly with '''<code>myString:=Memo1.Lines[Index];</code>'''. Note, the index of ''TMemo.Lines'' is zero based, i.e. the first line would be: '''<code>myString:=Memo1.Lines[0];</code>'''
+
Si voulez savoir ce qu'il y a dans une ligne particulière, vous pouvez le voir directement avec '''<code>myString := Memo1.Lines[Index];</code>'''. Remarquez que l'index de ''TMemo.Lines'' est basé sur zéro i.e. la première ligne sera '''<code>myString := Memo1.Lines[0];</code>'''
  
The preceding example add yet a TButton ''Button2'', you can display the third line as follows:
+
Dans l'exemple précédent, ajoutez un nouveau TButton ''Button2'', vous pouvez afficher la troisième ligne comme suit :
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button2Click(Sender: TObject);
 
procedure TForm1.Button2Click(Sender: TObject);
 
begin
 
begin
Line 55: Line 54:
  
 
== Texte sélectionné ==
 
== Texte sélectionné ==
You can mark text parts at run time by holding the left mouse button or press and hold the [Shift] key and select the text with the mouse or keyboard. This text ([[String]]) you can display like this:
+
Vous pouvez marquer (sélectionner) des parties de texte à l'exécution en maintenant enfoncé le bouton gauche de la souris ou en pressant sur la touche {{keypress|Shift}} et en sélectionnant le texte avec la souris ou le clavier. Vous pouvez afficher cette partie de texte en faisant cela :
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button2Click(Sender: TObject);
 
procedure TForm1.Button2Click(Sender: TObject);
 
begin
 
begin
Line 64: Line 63:
  
 
== Rechercher du texte ==
 
== Rechercher du texte ==
 +
Contrairement à l'exemple précédent, vous pouvez aussi rechercher visuellement un texte dans un mémo et recupérer sa position '''<code>Position := Memo1.SelStart;</code>'''
  
Contrary to the previous example, you can also looking for a text ([[String]]) in a TMemo and return the place where it is: '''<code>Position:=Memo1.SelStart;</code>'''
+
L'exemple suivant montre comment vous pouvez rechercher et rechercher encore un texte dans un mémo :
 
+
* Créez une nouvelle application avec un [[TEdit/fr|TEdit]] ''Edit1'', un TMemo ''Memo1'' et deux [[TButton/fr|TButton]] ''Button1'' et ''Button2''.
The following example shows how you can search and search further for a text in a memo:
+
* Complétez la clause uses avec '''LCLProc''' et '''strutils'''.
* Create a new application with a [[TEdit]] ''Edit1'', a TMemo ''Memo1'' and two [[TButton]] ''Button1'' and ''Button2''.
+
* Dans le gestionnaire de l'événement ''OnClick'' de ''Button1'', remplissez le mémo avec du texte, comme dans l'exemple [[#Ins.C3.A9rer_des_lignes_directement|Insérer des lignes directement]].
* Complete the uses clause to '''LCLProc''' and '''strutils'''.
+
* Dans l'éditeur de code source, ajoutez la fonction suivante (basée sur l'[http://www.lazarusforum.de/viewtopic.php?p=39260#p39260 exemple] issu du forum Lazarus allemand) :
* In the event handler ''OnClick'' of ''Button1'' fill the memo with any text, as in the example [[TMemo#Insert lines directly|Insert lines directly]].
+
<syntaxhighlight lang=pascal>
* In the source text editor add following function (based on the [http://www.lazarusforum.de/viewtopic.php?p=39260#p39260 example] from the German Lazarusforum):
 
<syntaxhighlight>
 
 
// FindInMemo: Returns the position where the string to search was found
 
// FindInMemo: Returns the position where the string to search was found
 
function FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;
 
function FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;
Line 85: Line 83:
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
* Now, add following code in the event handler ''OnClick'' from ''Button2'':
+
* Maintenant, ajoutez le code suivant dans le gestionnaire d'événement ''OnClick'' depuis ''Button2'' :
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button2Click(Sender: TObject);
 
procedure TForm1.Button2Click(Sender: TObject);
 
const
 
const
Line 104: Line 102:
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
* Now at run time, you can fill the memo with a text with ''Button1'', paste the text to be searched in ''Edit1'' and looking or keep looking for these in memo with ''Button2''.
+
* Maintenant à l'exécution, vous pouvez remplir le mémo avec ''Button1'', coller le texte à chercher dans ''Edit1'' et rechercher puis continuer à chercher dans ce mémo avec ''Button2''.
  
 
== Enregistrer et charger ==
 
== Enregistrer et charger ==
You can quite easily save and load the contents of a memo by using the methods ''SaveToFile'' and ''LoadFromFile'' of the class [[TStringList-TStrings Tutorial|TStrings]].
+
Vous pouvez tout aussi facilement enregistrer et charger le contenu d'un mémo en utilisant les méthodes ''SaveToFile'' et ''LoadFromFile'' de la classe [[TStringList-TStrings Tutorial/fr|TStrings]].
  
The following example shows you how:
+
L'exemple suivant vous montre comment :
* Create a new application with a TMemo ''Memo1'' and three [[TButton]] ''Button1'', ''Button2'' and ''Button3''.
+
* Créez une nouvelle application avec le TMemo ''Memo1''et trois [[TButton/fr|TButton]] ''Button1'', ''Button2'' et ''Button3''.
* Additionally put a [[TSaveDialog]] and a [[TOpenDialog]] from the component palette ''Dialogs'' on the form.
+
* Ajoutez un [[TSaveDialog/fr|TSaveDialog]] et un [[TOpenDialog/fr|TOpenDialog]] depuis la palette de composants ''Dialogs'' sur la fiche.
* Change Caption of ''Button1'' to "Fill memo".
+
* Modifiez l'intitulé de ''Button1'' en "Fill memo".
* In the event handler ''OnClick'' of ''Button1'' fill the memo with any text, as in the example [[TMemo#Insert lines directly|Insert lines directly]].
+
* Dans le gestionnaire d'événement ''OnClick'' de ''Button1'', remplissez le mémo avec du texte, comme dans l'exemple [[#Ins.C3.A9rer_des_lignes_directement|Insérer des lignes directement]].
* Change Caption of ''Button2'' to "Save memo".
+
* Modifiez l'intitulé du ''Button2'' en "Save memo".
* Change Caption of ''Button3'' to "Load memo".
+
* Modifiez l'intitulé du ''Button3'' en "Load memo".
* now modify the event handler ''OnClick'' of the buttons:
+
* maintenant modifiez les gestionnaires d'événement ''OnClick'' des boutons :
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure TForm1.Button2Click(Sender: TObject);
 
procedure TForm1.Button2Click(Sender: TObject);
 
begin
 
begin

Latest revision as of 10:50, 1 March 2020

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

Un TMemo tmemo.png est un contrôle d'édition de texte sur plusieurs lignes. Il est disponible depuis l'onglet Standard de la palette de composants.

Usage

Pour utiliser un TMemo sur une fiche, vous pouvez simplement le sélectionner dans la palette Standard et le placer en cliquant sur la fiche. Dans cette zone de saisie, vous pouvez maintenant éditer un texte multi-ligne à l'exécution.

Par exemple, dès que vous avez ajouté un TMemo Memo1 à votre fiche Form1, vous pouvez utilisez Memo1.Text:='this is a singleline text'; pourt affecter une chaîne.

Vous pouvez aussi utiliser le texte partout dans le code source le texte stocké dans Memo1 avec myString := Memo1.Text;.

Il est aussi possible d'affecter un texte multi-ligne avec Memo1.Text := 'This'+LineEnding+'is'+LineEnding+'a'+LineEnding+'multiline'+LineEnding+'text';.

Affectation d'une TStrings ou d'une TStringList

Un usage courant pour assigner un texte à un TMemo est l'utilisation d'un TStringList ou de son parentTStrings. L'exemple suivant montre ceci (dans le gestionnaire d'événement d'un TButton Button1 inséré dans la fiche Form1 avec un TMemo Memo1 posé dessus).

procedure TForm1.Button1Click(Sender: TObject);
var
  myStringList: TStringList;
begin
  myStringList:=TStringList.Create;               //Create my StringList
  myStringList.Add('This is the first line.');    //add a line
  myStringList.Add('This is the second line.');
  myStringList.Add('This is the third line.');
  myStringList.Add('etc.');
  Memo1.Lines.Assign(myStringList);               //assign text content
  myStringList.Free;                              //free my StringList
end;

Insérer des lignes directement

Vous pouvez ajouter directement le contenu du mémo, par exemple :

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;                              //delete all lines of Memo1
  Memo1.Lines.Add('This is the first line.');     //add a line
  Memo1.Lines.Add('This is the second line.');
  Memo1.Lines.Add('This is the third line.');
  Memo1.Lines.Add('etc.');
end;

Lire une ligne

Si voulez savoir ce qu'il y a dans une ligne particulière, vous pouvez le voir directement avec myString := Memo1.Lines[Index];. Remarquez que l'index de TMemo.Lines est basé sur zéro i.e. la première ligne sera myString := Memo1.Lines[0];

Dans l'exemple précédent, ajoutez un nouveau TButton Button2, vous pouvez afficher la troisième ligne comme suit :

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(Memo1.Lines[2]);
end;

Texte sélectionné

Vous pouvez marquer (sélectionner) des parties de texte à l'exécution en maintenant enfoncé le bouton gauche de la souris ou en pressant sur la touche Shift et en sélectionnant le texte avec la souris ou le clavier. Vous pouvez afficher cette partie de texte en faisant cela :

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage(Memo1.SelText); 
end;

Rechercher du texte

Contrairement à l'exemple précédent, vous pouvez aussi rechercher visuellement un texte dans un mémo et recupérer sa position Position := Memo1.SelStart;

L'exemple suivant montre comment vous pouvez rechercher et rechercher encore un texte dans un mémo :

  • Créez une nouvelle application avec un TEdit Edit1, un TMemo Memo1 et deux TButton Button1 et Button2.
  • Complétez la clause uses avec LCLProc et strutils.
  • Dans le gestionnaire de l'événement OnClick de Button1, remplissez le mémo avec du texte, comme dans l'exemple Insérer des lignes directement.
  • Dans l'éditeur de code source, ajoutez la fonction suivante (basée sur l'exemple issu du forum Lazarus allemand) :
// FindInMemo: Returns the position where the string to search was found
function FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;
begin
  Result := PosEx(AString, AMemo.Text, StartPos);
  if Result > 0 then
  begin
    AMemo.SelStart := UTF8Length(PChar(AMemo.Text), Result - 1);
    AMemo.SelLength := Length(AString);
    AMemo.SetFocus;
  end;
end;
  • Maintenant, ajoutez le code suivant dans le gestionnaire d'événement OnClick depuis Button2 :
procedure TForm1.Button2Click(Sender: TObject);
const
  SearchStr: String = '';                     // The string to search for
  SearchStart: Integer = 0;                   // Last position of the string to search for
begin
  if SearchStr <> Edit1.Text then begin       // Falls sich der zu suchende String geändert hat
    SearchStart := 0;
    SearchStr := Edit1.Text;
  end;
  SearchStart := FindInMemo(Memo1, SearchStr, SearchStart + 1);

  if SearchStart > 0 then
    Caption := 'Found at position['+IntToStr(SearchStart)+']!'
  else
    Caption := 'No further finds!';
end;
  • Maintenant à l'exécution, vous pouvez remplir le mémo avec Button1, coller le texte à chercher dans Edit1 et rechercher puis continuer à chercher dans ce mémo avec Button2.

Enregistrer et charger

Vous pouvez tout aussi facilement enregistrer et charger le contenu d'un mémo en utilisant les méthodes SaveToFile et LoadFromFile de la classe TStrings.

L'exemple suivant vous montre comment :

  • Créez une nouvelle application avec le TMemo Memo1et trois TButton Button1, Button2 et Button3.
  • Ajoutez un TSaveDialog et un TOpenDialog depuis la palette de composants Dialogs sur la fiche.
  • Modifiez l'intitulé de Button1 en "Fill memo".
  • Dans le gestionnaire d'événement OnClick de Button1, remplissez le mémo avec du texte, comme dans l'exemple Insérer des lignes directement.
  • Modifiez l'intitulé du Button2 en "Save memo".
  • Modifiez l'intitulé du Button3 en "Load memo".
  • maintenant modifiez les gestionnaires d'événement OnClick des boutons :
procedure TForm1.Button2Click(Sender: TObject);
begin
  if SaveDialog1.Execute then
    Memo1.Lines.SaveToFile(SaveDialog1.FileName);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
end;

Voir aussi

  • Doc. TMemo
  • TRichMemo - Comme le composant TRichEdit de Delphi : texte formaté (couleur, taille, etc.)
  • TListBox - Une liste de chaînes avec défilement


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