LISTBOX et OBJECT

Résolu
DENNLER Messages postés 6 Date d'inscription mardi 17 décembre 2002 Statut Membre Dernière intervention 15 septembre 2009 - 15 sept. 2009 à 08:39
DENNLER Messages postés 6 Date d'inscription mardi 17 décembre 2002 Statut Membre Dernière intervention 15 septembre 2009 - 15 sept. 2009 à 19:04
Bonjour,
je désire utiliser un élement de type Tcolor avec une listBox.

ListBox1.Items.AddObject (ColorToString (Colors[I]),TObject(Colors[I])); // Pour Créer un élément
ListBox1.Canvas.Font.Color := TColor(ListBox1.Items.Objects [Index]); // Pour Récupérer l'information

Mais comment modifier un enregistrement ??
TColor(ListBox1.Items.Objects [Index]):= ?????; // Erreur
D'avance merci

2 réponses

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
15 sept. 2009 à 17:16
sous Delphi < 2007

const
  Colors : array[0..1] of integer = ($00FF00, $FF0000);

procedure TForm7.FormCreate(Sender: TObject);
begin
  // Items[0] = 0000FF00
  // Objects[0] = 0x0000FF00
  ListBox1.Items.AddObject(format('%.8x',[Colors[0]]), Pointer(Colors[0]));

  // Objects[0] = 0x00FF0000
  ListBox1.Items.Objects[0] := Pointer(Colors[1]);

  // Color = $00FF0000
  color := integer(ListBox1.Items.Objects[0]);
end;



sous Delphi >= 2007

const
  MaxColors = (10 shl 20) div SizeOf(Integer);

type
  // Attention Colors utilise le champ Objects!
  // on utilise soit l'un, soit l'autre.
  TStringsHelper = class helper for TStrings
  private
    function GetColors(index: integer): integer;
    procedure SetColors(index: integer; const Value: integer);
  public
    function AddColor(const Str: string; const Color: integer): integer;
    property Colors[index: integer] : integer read GetColors write SetColors;
  end;



const
  Colors : array[0..1] of integer = ($00FF00, $FF0000);

procedure TForm7.FormCreate(Sender: TObject);
begin
  ListBox1.Items.AddColor(format('%.8x',[Colors[0]]), Colors[0]);

  ListBox1.Items.Colors[0] := Colors[1];

  color := ListBox1.Items.Colors[0];
end;



{ TStringsHelper }

function TStringsHelper.AddColor(const Str: string; const Color: integer): integer;
begin
  AddObject(Str, Pointer(Color));
end;

function TStringsHelper.GetColors(index: integer): integer;
begin
  result := integer(Objects[index]);
end;

procedure TStringsHelper.SetColors(index: integer; const Value: integer);
begin
  Objects[index] := pointer(Value);
end;
3
DENNLER Messages postés 6 Date d'inscription mardi 17 décembre 2002 Statut Membre Dernière intervention 15 septembre 2009
15 sept. 2009 à 19:04
Bonjour,
Merci pour cette réponse rapide qui me permet de mieux comprendre l'utilisation des objects dans une listbox.
0
Rejoignez-nous