Hi all,

I have a listbox that displays a list records from a file. The records contain details about a film - title, certificate, genre, whether or not its rented out or in.

At the moment, when a film is set to - rented out, that individual film is put into a seperate listbox from the rest of the films. instead of moving the film to a different listbox, is there a way of changing the colour of the film title?
So if the film is in the shop then the title of the film is in blue, if the film is out on rent, then the title of the film is red.

Could someone please help me out.

Katrix36

Dani AI

Generated

As described, the goal is to keep a single list and draw each title in a different color instead of moving items between lists. As suggested, the most direct, reliable way is to make the TListBox owner-drawn and pick the font color per item in OnDrawItem. The pattern below shows a compact, practical approach for Delphi/Pascal: store a small status in Items.Objects and draw each item in red (rented) or blue (available).

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListBox1.Style := lbOwnerDrawFixed;
  ListBox1.Items.AddObject('The Matrix', TObject(1)); // 1 = rented
  ListBox1.Items.AddObject('Toy Story',  TObject(0)); // 0 = available
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  LB: TListBox;
  txt: string;
  status: Integer;
begin
  LB := Control as TListBox;
  txt := LB.Items[Index];
  // selection gets system highlight
  if odSelected in State then
  begin
    LB.Canvas.Brush.Color := clHighlight;
    LB.Canvas.Font.Color := clHighlightText;
  end
  else
  begin
    status := Integer(LB.Items.Objects[Index]);
    if status = 1 then
      LB.Canvas.Font.Color := clRed
    else
      LB.Canvas.Font.Color := clBlue;
    LB.Canvas.Brush.Color := LB.Color;
  end;
  LB.Canvas.FillRect(Rect);
  LB.Canvas.TextOut(Rect.Left + 2, Rect.Top + 2, txt);
end;

Notes and pitfalls: casting an integer through Items.Objects works on classic 32-bit Delphi; for 64-bit compilers use NativeInt(...) or store a tiny TObject wrapper. If storing real objects, remember to free them on clear. After changing an item’s status, call ListBox1.Invalidate or Repaint to update the display. Increase ItemHeight when larger fonts are used.

If icons or multiple columns are desired, TListView with a small ImageList and state icons (or OnCustomDraw) is a better fit. For a simple rented/in-shop color flag, owner-draw TListBox is compact and easy to maintain.

Hi,

You can use a TChecklistbox to show out videos by a check; you can use TListview to assign different icons near items or you can set normal TListbox to ownerdraw and render the text for each element by yourself as you like.

Loren Soth

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.