Sign in Join
Wiki copytemporary

TListBox/fr: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: TListBox/fr

OldNewLine
1+Une **TListBox** ![](/wiki-images/7/77/Tlistbox.png) est un composant qui montre une liste défilable de chaîne (courtes) dans laquelle l'utilisateur peut en sélectionner une. Il est disponible depuis l'[[Standard tab/fr|onglet Standard]] de la [[Component Palette/fr|palette de composants]].
2+
3+Dans la TListBox, les [[String/fr|chaînes]] sont enregistrées dans la propriété *Items*, qui est du type [[TStrings/fr|TStrings]]. donc vous pouvez affecter ou retirer des chaînes dans la ListBox comme dans [[TStringList/fr|TStringList]] ou son parent [[TStringList-TStrings Tutorial/fr|TStrings]]
4+
5+Voici quelques exemples de comment utiliser une TListBox *ListBox1* sur une fiche *Form1* :
6+
7+# Remplir une ListBox
8+
9+## Avec l'inspecteur d'objet
10+
11+- Sélectionnez le ListBox sur votre fiche avec un clic.
12+- Allez dans l'inspecteur d'objet, dans l'onglet Propriétés et sur la propriété *Items*.
13+- Cliquez sur le bouton avec les trois points. L'éditeur de liste de chaînes s'ouvre.
14+- Entrez votre texte et confirmez le tout avec *OK*.
15+
16+## Par code via un clic de bouton
17+
18+Ajoutez sur votre fiche une [[TButton/fr|TButton]] nommé *btnFill* et un intitulé *Remplir la liste*. Dans le gestionnaire d'événement *OnClick* du bouton, vous écrivez le code suivant :
19+
20+``` pascal
21+procedure TForm1.btnFillClick(Sender: TObject);
22+begin
23+ ListBox1.Items.Clear; //Delete all existing strings
24+ ListBox1.Items.Add('First line');
25+ ListBox1.Items.Add('Line with random number '+IntToStr(Random(100)));
26+ ListBox1.Items.Add('Third line');
27+ ListBox1.Items.Add('Even a random number '+IntToStr(Random(100)));
28+end;
29+```
30+
31+# Affectation d'une StringList
32+
33+Ajoutez sur votre fiche un [[TButton/fr|TButton]] nommé *btnFill* intitulé *Remplir la liste*. Dans le gestionnaire d'événement *OnClick* du bouton, vous écrivez le code suivant :
34+
35+``` pascal
36+procedure TForm1.btnFillClick(Sender: TObject);
37+var
38+ myStringList: TStringList;
39+begin
40+ myStringList:=TStringList.Create; //Create my StringList
41+ try
42+ myStringList.Add('This is the first line.'); //This add a row
43+ myStringList.Add('This is the second first line.');
44+ myStringList.Add('This is the third line.');
45+ myStringList.Add('etc.');
46+ ListBox1.Items.Assign(myStringList); //assign the ListBox1 the text content of my StringList
47+ finally
48+ myStringList.Free; //Free my StringList in memory
49+ end;
50+end;
51+```
52+
53+# Ajout d'une chaîne
54+
55+- Etendez l'exemple [[TListBox/fr|Remplir un ListBox dans un clic de bouton]] en ajoutant un [[TEdit/fr|TEdit]] et un [[TButton/fr|TButton]] nommé *btnAdd* et intitulé *Ajouter une chaîne*. Effacez la propriété *Text*.
56+- Dans le gestionnaire d'événement *OnClick* du bouton, vous écrivez le code suivant :
57+
58+``` pascal
59+procedure TForm1.btnAddClick(Sender: TObject);
60+begin
61+ ListBox1.Items.Add(Edit1.Text);
62+ Edit1.Text:='';
63+end;
64+```
65+
66+# Supprimer une chaîne
67+
68+Par défaut, vous pouvez seulement sélectionner une seule ligne dans la liste. En passant la propriété *MultiSelect* à True, vous pouvez sélectionner plusieurs lignes de la liste.
69+
70+## Un ItemIndex
71+
72+- Etendez l'exemple [[TListBox/fr|Ajout d'une chaîne]] en ajoutant un [[TButton/fr|TButton]] nommé "btnDel" et intitulé "Supprimer la chaîne".
73+- Dans le gestionnaire d'événement *OnClick* du bouton, vous écrivez le code suivant :
74+
75+``` pascal
76+procedure TForm1.btnDelClick(Sender: TObject);
77+begin
78+ if ListBox1.ItemIndex > -1 then //Delete only when a string in the listbox is selected
79+ ListBox1.Items.Delete(ListBox1.ItemIndex);
80+end;
81+```
82+
83+## Toutes les chaînes sélectionnées
84+
85+- Etendez l'exemple [[TListBox/fr|Ajout d'une chaîne]] en ajoutant un [[TButton/fr|TButton]] nommé "btnDel" et intitulé "Supprimer la chaîne".
86+- Dans le gestionnaire d'événement *OnClick* du bouton, vous écrivez le code suivant :
87+
88+``` pascal
89+procedure TForm1.btnDelClick(Sender: TObject);
90+var
91+ i: Integer;
92+begin
93+ if ListBox1.SelCount > 0 then //Delete only if at least one string in the list box is selected
94+ for i:=ListBox1.Items.Count - 1 downto 0 do //Iterate through all the items
95+ if ListBox1.Selected[i] then //If selected...
96+ ListBox1.Items.Delete(i); //...delete the item (String)
97+end;
98+```
99+
100+# ListBox dessinée par le propriétaire
101+
102+En général, il est préférable de laisser le ListBox suivre le [thème](http://en.wikipedia.org/wiki/Skin_%28computing%29) défini par l'utilisateur. Dans certains cas (par exemple to programmer un jeu avec une surface toute colorée), vous pouvez vous détourner de ce standard et dessiner le contrôle selon votre propre choix. Vous pouvez essayer ceci maintenant :
103+
104+- Vous pouvez modifier l'exemple précédent ou créer une nouvelle application avec un TListBox *ListBox1*.
105+- Dans l'inspecteur d'objet, modifiez la propriété *Style* de *ListBox1* vers *lbOwnerDrawFixed*.
106+- Créez depuis l'inspecteur d'objet, onglet événements, le gestionnaire d'événement de *OnDrawItem*, en cliquant sur le bouton \[...\].
107+- Vous ajoutez le code suivant dans le gestionnaire :
108+
109+``` pascal
110+procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
111+ ARect: TRect; State: TOwnerDrawState);
112+var
113+ aColor: TColor; //Background color
114+begin
115+ if (Index mod 2 = 0) //Index tells which item it is
116+ then aColor:=$FFFFFF //every second item gets white as the background color
117+ else aColor:=$EEEEFF; //every second item gets pink background color
118+ if odSelected in State then aColor:=$0000FF; //If item is selected, then red as background color
119+ ListBox1.Canvas.Brush.Color:=aColor; //Set background color
120+ ListBox1.Canvas.FillRect(ARect); //Draw a filled rectangle
121+
122+ ListBox1.Canvas.Font.Bold:=True; //Set the font to "bold"
123+ ListBox1.Canvas.TextRect(ARect, 2, ARect.Top+2, ListBox1.Items[Index]); //Draw Itemtext
124+end;
125+```
126+
127+> **Note:** **Paramètres deListBoxDrawItem:**\
128+> \
129+> **Control:**\
130+> Si plusieurs contrôles (p.ex. de multiples ListBox) ont accès à ce gestionnaire, vous savez lequel a déclenché l'événement. Vous pouvez dans votre exemple au lieu de **`ListBox1.Canvas.FillRect(ARect)`** écrire aussi **`TListBox(Control).Canvas.FillRect(ARect)`**, où vous devriez vérifier s'il s'agit d'un ListBox :
131+>
132+> ``` pascal
133+> if Control is TListBox then
134+> TListBox(Control).Canvas.FillRect(ARect);
135+> ```
136+>
137+> **Index:**\
138+> Spécifie l'emplacement de l'article, vous avez ainsi accès à la chaîne **`<ListBox>.Items[Index]`**.\
139+> **ARect:**\
140+> Décrit le rectangle, ce qui est nécessaire pour le dessin du fond.\
141+> **State:**\
142+> Etat de l'article, soit normal, mis en évidence (focus), sélectionné etc.
143+
144+- Votre exemple pourrait ressembler à ceci :
145+
146+![](/wiki-images/2/26/ListBoxBsp1.png) -\> ![](/wiki-images/9/9f/ListBoxBsp2.png)
147+
148+# Voir aussi
149+
150+- [[doc:lcl/stdctrls/tlistbox.html|Doc. TListBox]]
151+- [[TDBListBox/fr|TDBListBox]] Ce composant rendu sensible aux données.
152+
153+---
154+*From the Free Pascal wiki, page "TListBox/fr", as it stood on 2020-03-01 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](/wiki-contact).*

Back to history