Solved

Make A Combo Box List With MultipleColors?

Posted on 2000-03-21
18
340 Views
Last Modified: 2010-04-04
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
Comment
Question by:CHORN
  • 7
  • 3
  • 3
  • +3
18 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2642209
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
 
LVL 17

Accepted Solution

by:
inthe earned 150 total points
ID: 2642230
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
 
LVL 2

Expert Comment

by:kubeerja
ID: 2642291
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
 
LVL 13

Expert Comment

by:Epsylon
ID: 2642296
Here you can find some nice components:

http://www.tmssoftware.com
0
 
LVL 2

Expert Comment

by:kubeerja
ID: 2642300
you have to set combobox style to csOwnerDrawVariable or csownderdrawfixed
0
 
LVL 17

Expert Comment

by:inthe
ID: 2642335
mmm ..
0
 

Author Comment

by:CHORN
ID: 2642491
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
 
LVL 17

Expert Comment

by:inthe
ID: 2642594
did you try the code i posted?
it works with no errors on d4.

what code gave the error ?

0
 

Author Comment

by:CHORN
ID: 2642702
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Expert Comment

by:rwilson032697
ID: 2642858
Can you post the code you are using?
0
 
LVL 2

Expert Comment

by:kotik
ID: 2643824
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
 

Author Comment

by:CHORN
ID: 2645063
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
 
LVL 2

Expert Comment

by:kotik
ID: 2645312
After creation simply add this code:

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

Regards,
Kot.
0
 

Author Comment

by:CHORN
ID: 2646265
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
 

Author Comment

by:CHORN
ID: 2650855
Adjusted points from 100 to 150
0
 

Author Comment

by:CHORN
ID: 2650856
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
 

Author Comment

by:CHORN
ID: 2651077
Thanks InThe
The TextOut method was raising the error
I mentioned above, but the DrawText
method does not.
Thanks again.
0
 
LVL 2

Expert Comment

by:kotik
ID: 2653869
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now