Difference between revisions of "TListBox/fr"
Line 47: | Line 47: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | = | + | = Ajout d'une chaîne = |
− | * | + | * Etendez l'exemple [[TListBox/fr#Par_code_via_un_clic_de_bouton|Remplir un ListBox dans un clic de bouton]] vers un [[TEdit/fr|TEdit]] et un [[TButton/fr|TButton]] nommé ''btnAdd'' et intitulé ''Ajouter une chaîne''. Effacez la propriété ''Text''. |
− | * | + | * Dans le gestionnaire d'événement ''OnClick'' du bouton, vous écrivez le code suivant : |
− | < | + | <syntaxhighlight> |
procedure TForm1.btnAddClick(Sender: TObject); | procedure TForm1.btnAddClick(Sender: TObject); | ||
begin | begin | ||
Line 56: | Line 56: | ||
Edit1.Text:=''; | Edit1.Text:=''; | ||
end; | end; | ||
− | </ | + | </syntaxhighlight> |
= Supprimer une chaîne = | = Supprimer une chaîne = |
Revision as of 07:43, 11 August 2017
│
Deutsch (de) │
English (en) │
suomi (fi) │
français (fr) │
日本語 (ja) │
Une TListBox 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'onglet Standard de la palette de composants.
Dans la TListBox, les chaînes sont enregistrées dans la propriété Items, qui est du type TStrings. donc vous pouvez affecter ou retirer des chaînes dans la ListBox comme dans TStringList ou son parent TStrings
Voici quelques exemples de comment utiliser une TListBox ListBox1 sur une fiche Form1 :
Remplir une ListBox
Avec l'inspecteur d'objet
- Sélectionnez le ListBox sur votre fiche avec un clic.
- Allez dans l'inspecteur d'objet, dans l'onglet Propriétés et sur la propriété Items.
- Cliquez sur le bouton avec les trois points. L'éditeur de liste de chaînes s'ouvre.
- Entrez votre texte et confirmez le tout avec OK.
Par code via un clic de bouton
Ajoutez sur votre fiche une TButton nommé btnFill et un intitulé Remplir la liste. Dans le gestionnaire d'événement OnClick du bouton, vous écrivez le code suivant :
procedure TForm1.btnFillClick(Sender: TObject);
begin
ListBox1.Items.Clear; //Delete all existing strings
ListBox1.Items.Add('First line');
ListBox1.Items.Add('Line with random number '+IntToStr(Random(100)));
ListBox1.Items.Add('Third line');
ListBox1.Items.Add('Even a random number '+IntToStr(Random(100)));
end;
Affectation d'une StringList
Ajoutez sur votre fiche un TButton nommé btnFill intitulé Remplir la liste. Dans le gestionnaire d'événement OnClick du bouton, vous écrivez le code suivant :
procedure TForm1.btnFillClick(Sender: TObject);
var
myStringList: TStringList;
begin
myStringList:=TStringList.Create; //Create my StringList
try
myStringList.Add('This is the first line.'); //This add a row
myStringList.Add('This is the second first line.');
myStringList.Add('This is the third line.');
myStringList.Add('etc.');
ListBox1.Items.Assign(myStringList); //assign the ListBox1 the text content of my StringList
finally
myStringList.Free; //Free my StringList in memory
end;
end;
Ajout d'une chaîne
- Etendez l'exemple Remplir un ListBox dans un clic de bouton vers un TEdit et un TButton nommé btnAdd et intitulé Ajouter une chaîne. Effacez la propriété Text.
- Dans le gestionnaire d'événement OnClick du bouton, vous écrivez le code suivant :
procedure TForm1.btnAddClick(Sender: TObject);
begin
ListBox1.Items.Add(Edit1.Text);
Edit1.Text:='';
end;
Supprimer une chaîne
By default is set that you can select only one row in your list box. Do you want to select several of the lines in your ListBox, you would have the property MultiSelect to make True.
A ItemIndex
- Extend the example Add string to a TButton with the name "btnDel" and caption "delete string".
- In the event handler OnClick of the button, you write the following code:
procedure TForm1.btnDelClick(Sender: TObject);
begin
if ListBox1.ItemIndex > -1 then //Delete only when a string in the listbox is selected
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
Toutes les chaînes sélectionnées
- Extend the example Add string to a TButton with the name "btnDel" and caption "delete string".
- In the event handler OnClick of the button, you write the following code:
procedure TForm1.btnDelClick(Sender: TObject);
var
i: Integer;
begin
if ListBox1.SelCount > 0 then //Delete only if at least one string in the list box is selected
for i:=ListBox1.Items.Count - 1 downto 0 do //Iterate through all the items
if ListBox1.Selected[i] then //If selected...
ListBox1.Items.Delete(i); //...delete the item (String)
end;
ListBox dessinée par le propriétaire
In general, it is advantageous to let the ListBox follow the theme set by the user. In some cases (for example, to program a game with a colorful surface), you can deviate from this standard and draw the control according to your own choice. You can try this now:
- You can modify the previous sample or create a new application with a TListBox ListBox1.
- In the Object Inspector, change ListBox1 property Style to lbOwnerDrawFixed.
- With the Object Inspector, tab events, create the event handler for the event OnDrawItem, by clicking on the button [...].
- You add the following code to the handler:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
var
aColor: TColor; //Background color
begin
if (Index mod 2 = 0) //Index tells which item it is
then aColor:=$FFFFFF //every second item gets white as the background color
else aColor:=$EEEEFF; //every second item gets pink background color
if odSelected in State then aColor:=$0000FF; //If item is selected, then red as background color
ListBox1.Canvas.Brush.Color:=aColor; //Set background color
ListBox1.Canvas.FillRect(ARect); //Draw a filled rectangle
ListBox1.Canvas.Font.Bold:=True; //Set the font to "bold"
ListBox1.Canvas.TextRect(ARect, 2, ARect.Top+2, ListBox1.Items[Index]); //Draw Itemtext
end;
Remarque: Parameters of ListBoxDrawItem:
Control:
If multiple controls (E.g. multiple ListBoxes) access this event handle, you know which threw this event. You could in our example, instead of ListBox1.Canvas.FillRect(ARect)
also TListBox(Control).Canvas.FillRect(ARect)
write, where you should query still possible before, whether it is a TListBox:
if Control is TListBox then
TListBox(Control).Canvas.FillRect(ARect);
Index:
Specifies the item location, so you have access to the string <ListBox>.Items[Index]
.
ARect:
Describes the rectangle, which is necessary for drawing the background.
State:
Status of the items, whether normal, focused, selected etc.
- Your example could look like:
Voir aussi
- Doc. TListBox
- TDBListBox Ce composant rendu sensible aux données.