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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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
kretzschmarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
umuligAuthor Commented:
Sorry Mottor , but kretzschmar's solv seems better to me.
0
mottorCommented:
That's true.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.