Le composant TListBox permet de stocker d'autres choses que des chaines de caractères.
Exemple : soit une fiche avec un bouton (Button1), un TLabel (Label1) et un TListBox (ListBox1) :
Dans le gestionnaire d'évènements OnClick du bouton, on remplit ListBox1 avec des chaines mais aussi avec des chaines issues d'un tableau
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
const
tab : array[0..5] of string = ('zéro','un','deux','trois','quatre','cinq');
begin
ListBox1.Clear;
for i:= low(tab) to High(Tab) - 1 do
ListBox1.Items.AddObject(IntToStr(i), TObject(Tab[i]));
end;
Ensuite, dans l'évènement OnClick de ListBox1, on désire afficher la valeur littérale de l'élément sélectionné :
procedure TForm1.ListBox1Click(Sender: TObject);
begin
with ListBox1 do
if ItemIndex <> -1 then
Label1.Caption := String(Items.Objects[ItemIndex]);
end;
Ainsi, même en triant les éléments visibles dans ListBox1, les "Objects" suivent et tu ne perds pas la trace.
Pour le vérifier, ajoutons deux boutons nommés btnUp (remonte un élément dans la liste) et btnDown (descend un élément dans la liste). Voici le code de leurs évènements OnClick repectifs :
procedure TForm1.btnUpClick(Sender: TObject);
begin
with ListBox1 do
if (ItemIndex <> -1) and (ItemIndex > 0) then
Items.Exchange(ItemIndex, ItemIndex - 1);
end;
procedure TForm1.btnDownClick(Sender: TObject);
begin
with ListBox1 do
if (ItemIndex <> -1) and (ItemIndex < Items.Count - 1) then
Items.Exchange(ItemIndex, ItemIndex + 1);
end;
Après vérification, tout fonctionne nickel.
May Delphi be with you