• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

Make A Combo Box List With MultipleColors?

Is it possible to make the entries
in a combo box list different colors?
For instance if the combo box has
Fred, Wilma, and Scoobe as choices
how would I make Scoobe a different color than Fred and Wilma?
0
CHORN
Asked:
CHORN
  • 7
  • 3
  • 3
  • +3
1 Solution
 
rwilson032697Commented:
You can provide your own item drawer by setting the style to csOwnerDrawFixed and adding this event handler:

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  if Index = 1 then
    Combobox1.canvas.font.color := clRed
  else
    Combobox1.canvas.font.color := clBlack;
  Combobox1.canvas.textout(rect.left + 2, rect.top + 2, Combobox1.items[index]);
end;

This is incomplete as it does not draw the focus rectangle etc, but it gives you a start...

Cheers,

Raymond.
0
 
intheCommented:
hi,

set the style to csownderdrawfixed in object inspector then :

procedure TForm1.FormCreate(Sender: TObject);
begin
 with ComboBox1.Items do
 begin
    Add('Fred');
    Add('Wilma');
    Add('Barney');
    Add('Betty');
    Add('Bam Bam');
   end;
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
itemstr : string;
colors : array[0..4]of string;
 begin
colors[0] := inttostr(clRed);
colors[1] := inttostr(clBlue);
colors[2] := inttostr(clFuchsia);
colors[3] := inttostr(clTeal);
colors[4] := inttostr(clGray);
 with Control as tComboBox,Canvas do begin
  Brush.Color := clWhite;
   FillRect(Rect);
   InflateRect(Rect,-2,-2);
    Brush.Color := StrToInt(colors[Index]);
    Brush.Style := bsBDiagonal;
   FillRect(Rect);
   itemStr := ComboBox1.Items.Strings[Index];
    DrawText(Combobox1.Canvas.Handle,pchar(itemstr),Length(itemstr),
      Rect, DT_LEFT and DT_VCENTER and DT_SINGLELINE);
   end;
 end;

Regards Barry
0
 
kubeerjaCommented:
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
const CharOffset = 2;
begin
  with ComboBox1.Canvas do
  begin
      if ComboBox1.Items[index]='Scoobe'  then
      begin
        brush.Color:= clTeal ;
        font.color := clWhite ;
      end
      else
      begin
        brush.Color:= clYellow ;
        font.color := clRed ;
      end;
    FillRect(Rect);
    textout(rect.left+CharOffset, rect.top+CharOffset, ComboBox1.Items[index]);
  end
end;
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
EpsylonCommented:
Here you can find some nice components:

http://www.tmssoftware.com
0
 
kubeerjaCommented:
you have to set combobox style to csOwnerDrawVariable or csownderdrawfixed
0
 
intheCommented:
mmm ..
0
 
CHORNAuthor Commented:
In my example when I change the color
to red for scoobe I get an error telling me 'Canvas Does Not Allow Drawing'.  Does anyone know why this error is occuring ?
0
 
intheCommented:
did you try the code i posted?
it works with no errors on d4.

what code gave the error ?

0
 
CHORNAuthor Commented:
The textout procedure raises the error
when I select scoobe.
I am using this in a VCL that I derived
from TCustomCombo.  When I initialize the control I set it's style as you havr mentioned.  In the control I am overriding the DrawItem event.
Thanks for your help.
0
 
rwilson032697Commented:
Can you post the code you are using?
0
 
kotikCommented:
Do you create your ComboBox at runtime? You have to set the Parent property in that case to be able to draw on the canvas.

Regards,
Kot.
0
 
CHORNAuthor Commented:
I do create the combobox at runtime.
What method do I need to look at in order to let the parent draw on the canvas ?
0
 
kotikCommented:
After creation simply add this code:

  ComboBox.Parent := self;  // the owner form

Regards,
Kot.
0
 
CHORNAuthor Commented:
This code is from a VCL.
in the protected section of the implementation I have the following:
protected
 { Protected declarations }
  procedure Change; override;
  procedure DrawItem(Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
  override;
  procedure KeyDown(var Key: Word;
  Shift: TShiftState); override;


Then in the implementation
the procedure looks like this :

procedure TLookupList.DrawItem(Index: Integer; Rect: TRect; State: TOwnerDrawState);
begin
   if TCodeList(Items.Objects[Index]).Valid then
      Font.color := clBlack
   else
      Font.color := clRed;
   //inherited DrawItem(Index, Rect, State);
   try
      Canvas.FillRect(Rect);
      Canvas.TextOut(rect.left, rect.top, Items[index]);
   except
   end;
end;

0
 
CHORNAuthor Commented:
Adjusted points from 100 to 150
0
 
CHORNAuthor Commented:
Thanks for all the help everyone.
I will award the points to the first person who can explain how to change the
font color while the item is selected.
I have tries changing the font when the state is odSelected but it seems like the focus rectangle has it's own font property.
Again Thanks for all the help.
0
 
CHORNAuthor Commented:
Thanks InThe
The TextOut method was raising the error
I mentioned above, but the DrawText
method does not.
Thanks again.
0
 
kotikCommented:
I have no problems with this code:

with (Control as TListBox).Canvas do
begin
  { display the text }
  Font.Name := 'Verdana';
  Font.Size := 8;
  if odSelected in (State) then
    Font.Color := clYellow
  else
    Font.Color := clNavy;
  TextOut(Rect.Left + 18, Rect.Top + 4, GetItemText));
end;
Regards,
Kot.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 7
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now