* * *

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

BorisSobad

  • New member
  • *
  • Posts: 16
Color Different Lines of Text in Listbox
« on: February 03, 2012, 09:27:40 pm »
I used  this:
procedure TForm3.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
    myColor: TColor;
    myBrush: TBrush;     
begin
  myBrush := TBrush.Create; 
  with (Control as TListBox).Canvas do // draw on control canvas, not on the form
  begin
    if Index = 3 then
      myColor := clRed
    else
      myColor := clBlack;

    myBrush.Style := bsSolid;
    myBrush.Color := myColor;

    Windows.FillRect(handle, Rect, myBrush.Handle);

    Brush.Style := bsClear; 
    // display the text
    TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[Index]); 
    MyBrush.Free;
  end;
end;

Doesn't work. Total Noob.

circular

  • Hero Member
  • *****
  • Posts: 808
Re: Color Different Lines of Text in Listbox
« Reply #1 on: February 03, 2012, 10:09:58 pm »
Your code does not look so bad.

Did you set Style property of ListBox to lbOwnerDrawFixed ?
Conscience is the debugger of the mind

User137

  • Hero Member
  • *****
  • Posts: 503
Re: Color Different Lines of Text in Listbox
« Reply #2 on: February 03, 2012, 10:51:24 pm »
Instead of Windows.FillRect(), you can use canvas.Rectangle(). That would same time be crossplatform... try to avoid using Windows unit at all costs.

Instead of myBrush.Style you can use simply Brush.Style, inherited from "with (Control as TListBox).Canvas do".

BorisSobad

  • New member
  • *
  • Posts: 16
Re: Color Different Lines of Text in Listbox
« Reply #3 on: February 04, 2012, 01:33:33 am »
Yes I set the style to lbOwner.fixed
It was saying "windows" identifier not found. & wrong number of parameters specified to call to "Rect". After change at least 1st problem went away. Still have call to "Rect" problem. Also its says
'warning: symbol "handle" is deprecated.' Whatever that means.
« Last Edit: February 04, 2012, 02:13:42 am by BorisSobad »

User137

  • Hero Member
  • *****
  • Posts: 503
Re: Color Different Lines of Text in Listbox
« Reply #4 on: February 04, 2012, 02:03:07 pm »
I didn't test this, but this has those little fixes  :P

Code: [Select]
procedure TForm3.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  with (Control as TListBox).Canvas do // draw on control canvas, not on the form
  begin
    Brush.Style := bsSolid;     
    if Index = 3 then
      Brush.Color := clRed
    else
      Brush.Color := clBlack;

    Rectangle(Rect);

    Brush.Style := bsClear; 
    // display the text
    TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[Index]); 
  end;
end;

BorisSobad

  • New member
  • *
  • Posts: 16
Re: Color Different Lines of Text in Listbox
« Reply #5 on: February 04, 2012, 03:02:51 pm »
Okay thanks almost working now. I have red lines instead of red text.

Avishai

  • Sr. Member
  • ****
  • Posts: 360
Re: Color Different Lines of Text in Listbox
« Reply #6 on: February 04, 2012, 03:33:08 pm »
Try this:

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  ARect: TRect; State: TOwnerDrawState);
begin
  with (Control as TListBox).Canvas do
  begin
    case Index of
      3,7: Font.Color := clRed;
    else
      Font.Color := clBlack;
    end;
    TextOut(ARect.Left, ARect.Top, (Control as TListBox).Items[Index]);
    Font.Color := clBlack;
  end;
end;
« Last Edit: February 04, 2012, 03:37:58 pm by Avishai »
Lazarus-1.1-37100-fpc-2.6.1-win64

circular

  • Hero Member
  • *****
  • Posts: 808
Re: Color Different Lines of Text in Listbox
« Reply #7 on: February 04, 2012, 04:21:25 pm »
It was saying "windows" identifier not found. & wrong number of parameters specified to call to "Rect". After change at least 1st problem went away. Still have call to "Rect" problem.
You need to change the parameter name, for example to "ARect" so that there is no confusion with Rect function. You can also copy the value of Rect to a temporary variable "TheRect" at the beggining of the procedure, then use "TheRect".
Conscience is the debugger of the mind

BorisSobad

  • New member
  • *
  • Posts: 16
Re: Color Different Lines of Text in Listbox
« Reply #8 on: February 04, 2012, 04:53:27 pm »
Working with this. I could make it work "if index =" but when I tried "if myColor =" it changes whole list to color not just line desired. Can I do it without the index controlling? ::)


procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  with (Control as TListBox).Canvas do // draw on control canvas, not on the form
  begin
    Brush.Style := bsSolid;
    if Index = 2 then
   // if MyColor = 'Red' then
      Font.Color := clRed
    else
      Font.Color := clBlack;

    Rectangle(Rect);

    Brush.Style := bsClear;
    // display the text
    TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[Index]);
  end;
end;                             

User137

  • Hero Member
  • *****
  • Posts: 503
Re: Color Different Lines of Text in Listbox
« Reply #9 on: February 04, 2012, 10:24:57 pm »
Now its going to programming basics  ::)  You haven't told us what the code should do exactly. Also now you don't have brush.color set at all for background rectangle, so it's undefined (white by default?). If i recall, if you don't draw the background rectangle, then text will show scrambled when scrolling.

Anyway...
Font.Color changes the font color, used with TextOut(...)
Brush.Color changes the background color, used with Rectangle(...)
Index is the index of line that is being drawn
ListBox.ItemIndex is index of item that is selected

BorisSobad

  • New member
  • *
  • Posts: 16
Re: Color Different Lines of Text in Listbox
« Reply #10 on: February 04, 2012, 11:01:35 pm »
Okay. I have a program all finished except for 1 part. It is a logbook using a listbox. Before I enter a daily item into the logbook I want to indicate whether the line will be black(normal) or red(problem). So I want to be able to input black or red lines into the list box and save them to call up later. I don't want to make listbox(item 3) red, I would like the line to be red if it is a certain type. This is my first Pascal program would like to see it finished. Hope I'm making myself clear on what I need to do.

circular

  • Hero Member
  • *****
  • Posts: 808
Re: Color Different Lines of Text in Listbox
« Reply #11 on: February 04, 2012, 11:07:38 pm »
Well, instead of storing the actual text in each listbox item, you can store a reference to that logbook entry. For example, if you have an array of logbook entries, you can add in the listbox items containing the number of the entry in the array of loogbook entries.

Then, in the drawing event, you retrieve the entry using the number and set the color according to the content of the entry.
Conscience is the debugger of the mind

BorisSobad

  • New member
  • *
  • Posts: 16
Re: Color Different Lines of Text in Listbox
« Reply #12 on: February 05, 2012, 12:14:35 am »
 Nope still confused. No easy way to choose which lines are red and which are black?
« Last Edit: February 05, 2012, 04:11:33 pm by BorisSobad »

circular

  • Hero Member
  • *****
  • Posts: 808
Re: Color Different Lines of Text in Listbox
« Reply #13 on: February 05, 2012, 07:18:26 pm »
Where is stored the data that tells you if a line is red or black ?
Conscience is the debugger of the mind

BorisSobad

  • New member
  • *
  • Posts: 16
Re: Color Different Lines of Text in Listbox
« Reply #14 on: February 05, 2012, 08:01:01 pm »
I add a "~" to the end of the lines that are red . I have a global variable 'myColor' .  So if last char of line = "~" then the line should be red.
I made this work on Visual Basic. Maybe I'm trying to hard to do things similar.

 

Recent

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