* * *

Author Topic: Color Different Lines of Text in Listbox  (Read 1574 times)

User137

  • Hero Member
  • *****
  • Posts: 503
Re: Color Different Lines of Text in Listbox
« Reply #15 on: February 05, 2012, 11:27:48 pm »
So something like this in the ListBox1DrawItem()?

Code: [Select]
var s: string;

  s:=TListBox(Control).items[index];
  if copy(s, length(s), 1) = '~' then
    font.color:=clRed
  else
    font.color:=clBlack;
« Last Edit: February 05, 2012, 11:29:27 pm by User137 »

circular

  • Hero Member
  • *****
  • Posts: 808
Re: Color Different Lines of Text in Listbox
« Reply #16 on: February 06, 2012, 12:50:54 am »
Ah ok, so you can test with :
Code: [Select]
var theText: string;

begin
  ...
  theText := (Control as TListBox).Items[Index];
  if copy(theText, length(theText), 1) = '~' then
    ...
Conscience is the debugger of the mind

BorisSobad

  • New member
  • *
  • Posts: 16
Re: Color Different Lines of Text in Listbox
« Reply #17 on: February 07, 2012, 02:21:39 am »
 :D Yes. User 137's method worked. Had a problem w/ Circular's code. Maybe I entered something wrong but I didn't spend too much time after I got a method that worked. Thanks so much for the assistance guys. Program finished. :)

circular

  • Hero Member
  • *****
  • Posts: 808
Re: Color Different Lines of Text in Listbox
« Reply #18 on: February 08, 2012, 04:33:20 pm »
lol I didn't see that User 137 had already posted the code.
Conscience is the debugger of the mind

JdeHaan

  • Newbie
  • Posts: 2
Re: Color Different Lines of Text in Listbox
« Reply #19 on: February 08, 2012, 10:42:33 pm »
Additionally, for others needing different line colors + a different color for the selected item, I use the following:


procedure TForm1.FilelistboxDrawItem(Control: TWinControl; Index: Integer;
                                     aRect: TRect; State: TOwnerDrawState);
begin
  with (Control as TFileListBox).Canvas do begin
    Brush.Style := bsSolid;
    if not FileListBox.Selected[Index] then begin
      if not Odd(Index) then
        Brush.Color := clWhite
      else
        Brush.Color := RGBToColor(243, 246, 250);
    end else begin
      Brush.Color := RGBToColor(56, 117, 215);
      Font.Color := clWhite;
    end;
    FillRect(aRect);
    Brush.Style := bsClear;
    TextOut(aRect.Left, aRect.Top, (Control as TFileListBox).Items[Index]);
  end;
end;

Should also work for TListBox.
KR

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads