Change Background color in a ChecklistBox

I need to change the backgroundcolor for my first few items then have another color for my next items.

How can this be done ?

Source Plz.

Ted
LVL 1
umuligAsked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
hi umulig,

my version

//CkeckListBox-Property Style = lbOwnerDrawFixed
//count of othercolored items from begin
//count could be changed at runtime
var ColorRows : Integer = 5;

procedure TForm1.CheckListBox1DrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
  if not (odSelected in State) then  //if it not a selected Item
  begin
    if Index < ColorRows then        //if it to color with another color
    begin
      CheckListBox1.Canvas.Brush.Color := clRed;  //could also clBlue
      CheckListBox1.Canvas.FillRect(Rect);
      CheckListBox1.Canvas.TextOut(Rect.Left+2,Rect.Top+2,ChecklistBox1.Items[Index]);
    end
    else                             //else default color
    begin
      CheckListBox1.Canvas.Brush.Color := CheckListBox1.Color;
      CheckListBox1.Canvas.FillRect(Rect);
      CheckListBox1.Canvas.TextOut(Rect.Left+2,Rect.Top+2,ChecklistBox1.Items[Index]);
    end;
  end
  else                              //else selected color
  begin
    CheckListBox1.Canvas.Brush.Color := clHighLight;
    CheckListBox1.Canvas.FillRect(Rect);
    CheckListBox1.Canvas.TextOut(Rect.Left+2,Rect.Top+2,ChecklistBox1.Items[Index]);
  end;
end;

//change the amount of other colored items
procedure TForm1.Button1Click(Sender: TObject);
begin
  ColorRows := StrToInt(Edit1.Text);
  CheckListBox1.Repaint;
end;

meikl
0
 
edeyCommented:
Not sure what you mean, but most desc of TWinControl have a color property you can modify.


GL
Mike
0
 
mottorCommented:
Set Style to  lbOwnerDrawFixed

and use event OnDrawItem.


That's from Delphi help:

procedure TForm1.CheckListBox1DrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
      Bitmap: TBitmap;      { temporary variable for the item’s bitmap }
      Offset: Integer;      { text offset width }
begin
  with (Control as TCustomListBox).Canvas do  { draw on control canvas, not on the form }
  begin
    FillRect(Rect);       { clear the rectangle }
    Offset := 2;          { provide default offset }
    Bitmap := TBitmap((Control as TCustomListBox).Items.Objects[Index]);  { get the bitmap }
    if Bitmap <> nil then
    begin
      BrushCopy(Bounds(Rect.Left + 2, Rect.Top, Bitmap.Width, Bitmap.Height),
                  Bitmap, Bounds(0, 0, Bitmap.Width, Bitmap.Height), clRed);  {render bitmap}
      Offset := Bitmap.width + 6;    { add four pixels between bitmap and text}
    end;
    // change color here
    if Index < 4 then
      Font.color := clRed;
    TextOut(Rect.Left + Offset, Rect.Top, (Control as TCustomListBox).Items[Index])  { display the text }
  end;
end;
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
umuligAuthor Commented:
Can't see that this is what I asked for .
I'll try to make my Question More Clear.
I need some source to show me how i can have The first X number of items in my Tchecklistbox with a Blue back ground and the rest with the normal background color.

Can someone Plz show me som code doing just that ?

Regards

Ted
0
 
mottorCommented:
Instead Font.Color ,Chgange Brush.Color
0
 
mottorCommented:
procedure TForm1.CheckListBox1DrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
Bitmap: TBitmap;      { temporary variable for the item’s bitmap }
Offset: Integer;      { text offset width }
begin
  with (Control as TCustomListBox).Canvas do  { draw on control canvas, not on the form }
  begin
    // change color here X=4
    if Index < 4 then
      Brush.color := clBlue;
    FillRect(Rect);       { clear the rectangle }
    Offset := 2;          { provide default offset }
    Bitmap := TBitmap((Control as TCustomListBox).Items.Objects[Index]);  { get the bitmap }
    if Bitmap <> nil then
    begin
      BrushCopy(Bounds(Rect.Left + 2, Rect.Top, Bitmap.Width, Bitmap.Height),
       Bitmap, Bounds(0, 0, Bitmap.Width, Bitmap.Height), clRed);  {render bitmap}
      Offset := Bitmap.width + 6;    { add four pixels between bitmap and text}
    end;
    TextOut(Rect.Left + Offset, Rect.Top, (Control as TCustomListBox).Items[Index])  { display the text }
  end;
end;
0
 
umuligAuthor Commented:
Sorry Mottor , but kretzschmar's solv seems better to me.
0
 
mottorCommented:
That's true.
0
All Courses

From novice to tech pro — start learning today.