Change font color in a TComboxBox? [Edit]

I have a 5 row combo box showing, in a black font, the values
 'select a grade'
 '1'
 '2'
 '3'
 '4'

Using the OnDrawitem event I color the background of each row depending upon the text shown (1 = blue, 2 = green, 3 = yellow, 4 = red).
However the 'preselect' font color, when the mouse is over an item, and also the selected font color always changes to white. This does not show up on the yellow or green backgrounds.

How can I make the preselect and selected font color stay black (or even change depending on the backgroupd color)?

For info, the code I currently use to change the background color of the rows is below. The actual color is recovered from a database using a line like Frm_Options.GetGradeColour4;

Howard

{code}
procedure TStaffComplete.ComboBoxGradeDrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
 ColourRowsOfComboBox(Control, Index, Rect, State);
end;


procedure TStaffComplete.ColourRowsOfComboBox(Control: TWinControl;  Index: Integer; Rect: TRect; State: TOwnerDrawState);
//to do this it needs the style property of the combobox or listbox set to csOwnerDrawFixed
//then call this code in the OnDrawItem event
var
   myColor: TColor;
   myBrush: TBrush;
   Xposition:integer;
   linewidth:integer;
begin
   myBrush := TBrush.Create;
   with (Control as TcomboBox).Canvas do
      begin
      case Index of
      0: myColor := clwhite;
      1: mycolor := Frm_Options.GetGradeColour1;
      2: mycolor := Frm_Options.GetGradeColour2;
      3: mycolor := Frm_Options.GetGradeColour3;
      4: mycolor := Frm_Options.GetGradeColour4;
      end;

      myBrush.Style := bsSolid;              //setthe style of the background
      myBrush.Color := myColor;
      Windows.FillRect(handle, Rect, myBrush.Handle) ;//colour the background
      Brush.Style := bsClear;

      //centre the text
      linewidth := Rect.right - Rect.Left;
      Xposition := (linewidth div 2);
      if Index = 0 then
         TextOut(Rect.Left, Rect.Top,(Control as TcomboBox).Items[Index])  //left aligned
      else
         TextOut(Rect.Left+Xposition, Rect.Top,(Control as TcomboBox).Items[Index]) ; //centred
      end;
   MyBrush.Free;
end;
{code}

Edited by: howard manwaring on May 30, 2012 2:14 AM
0
howard
5/30/2012 9:14:54 AM
embarcadero.delphi.vcl.using 2297 articles. 1 followers. Follow

5 Replies
472 Views

Similar Articles

[PageSpeed] 0

On 30/05/2012 7:14 PM, howard manwaring wrote:
> I have a 5 row combo box showing, in a black font, the values
>   'select a grade'
>   '1'
>   '2'
>   '3'
>   '4'
>
> Using the OnDrawitem event I color the background of each row depending upon the text shown (1 = blue, 2 = green, 3 = yellow, 4 = red).
> However the 'preselect' font color, when the mouse is over an item, and also the selected font color always changes to white. This does not show up on the yellow or green backgrounds.
>
> How can I make the preselect and selected font color stay black (or even change depending on the backgroupd color)?
>
> For info, the code I currently use to change the background color of the rows is below. The actual color is recovered from a database using a line like Frm_Options.GetGradeColour4;
>
> Howard
>
> {code}
> procedure TStaffComplete.ComboBoxGradeDrawItem(Control: TWinControl; Index: Integer;
>    Rect: TRect; State: TOwnerDrawState);
> begin
>   ColourRowsOfComboBox(Control, Index, Rect, State);
> end;
>
>
> procedure TStaffComplete.ColourRowsOfComboBox(Control: TWinControl;  Index: Integer; Rect: TRect; State: TOwnerDrawState);
> //to do this it needs the style property of the combobox or listbox set to csOwnerDrawFixed
> //then call this code in the OnDrawItem event
> var
>     myColor: TColor;
>     myBrush: TBrush;
>     Xposition:integer;
>     linewidth:integer;
> begin
>     myBrush := TBrush.Create;
>     with (Control as TcomboBox).Canvas do
>        begin
>        case Index of
>        0: myColor := clwhite;
>        1: mycolor := Frm_Options.GetGradeColour1;
>        2: mycolor := Frm_Options.GetGradeColour2;
>        3: mycolor := Frm_Options.GetGradeColour3;
>        4: mycolor := Frm_Options.GetGradeColour4;
>        end;
>
>        myBrush.Style := bsSolid;              //setthe style of the background
>        myBrush.Color := myColor;
>        Windows.FillRect(handle, Rect, myBrush.Handle) ;//colour the background
>        Brush.Style := bsClear;
>
>        //centre the text
>        linewidth := Rect.right - Rect.Left;
>        Xposition := (linewidth div 2);
>        if Index = 0 then
>           TextOut(Rect.Left, Rect.Top,(Control as TcomboBox).Items[Index])  //left aligned
>        else
>           TextOut(Rect.Left+Xposition, Rect.Top,(Control as TcomboBox).Items[Index]) ; //centred
>        end;
>     MyBrush.Free;
> end;
> {code}
>
> Edited by: howard manwaring on May 30, 2012 2:14 AM

As you are changing the brush color, why don't you also change the Font 
color? Font.Color := myfontcolor.

Then, when you call TextOut, it'll use the correct color that you have 
specified.

HTH
Andrew
0
Andrew
5/30/2012 10:48:48 AM
> {quote:title=Andrew Baylis wrote:}{quote}

> As you are changing the brush color, why don't you also change the Font 
> color? Font.Color := myfontcolor.
> 
> Then, when you call TextOut, it'll use the correct color that you have 
> specified.
> 
> HTH
> Andrew

Thank you, that sort of helped.
Using font.color := clblack, the font stays black all the time, whether the mouse is over it or not, which is much better than changing from black to white as the mouse goes over a row and when a selection has been made.

Of course this means that now the user can no longer see which row is preselected (ie nothing appears to happen as they move the mouse which is a bit disconcerting) but with only four rows to choose from I don't suppose that matters too much.

I'd be interested to know if there is a way to change the preselect (and selected) font color independently of the base font color though so that for example the text in the rows ise shown in black and as you move the mouse over the rows the text changes to grey (or even a complimentary color tro that of the background).
Howard
0
howard
5/30/2012 1:09:12 PM
howard manwaring wrote:

> I have a 5 row combo box showing, in a black font, the values
>  'select a grade'
>  '1'
>  '2'
>  '3'
>  '4'
> 
> Using the OnDrawitem event I color the background of each row
> depending upon the text shown (1 = blue, 2 = green, 3 = yellow, 4 =
> red).  However the 'preselect' font color, when the mouse is over an
> item, and also the selected font color always changes to white. This
> does not show up on the yellow or green backgrounds.
> 
> How can I make the preselect and selected font color stay black (or
> even change depending on the backgroupd color)?

Your code completely ignores the value of the State parameter. That is
a big fault on your part, this parameter is there for a reason, it
tells you which state the items needs to be drawn in (normal, selected,
hotlight etc.). You need to adjust your background and foreground
(font) color according to state.




-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://codenewsfast.com
http://groups.google.com
0
Peter
5/30/2012 4:50:53 PM
howard wrote:

> However the 'preselect' font color, when the mouse is over an item,
> and also the selected font color always changes to white. This does
> not show up on the yellow or green backgrounds.

As Peter said, you need to look at the State parameter of the OnDrawItem 
event to know how to draw each item.  Try something like this:

{code:delphi}
procedure TStaffComplete.ComboBoxGradeDrawItem(Control: TWinControl; Index: 
Integer; Rect: TRect; State: TOwnerDrawState);
begin
  ColourRowsOfComboBox(Control, Index, Rect, State);
end;

procedure TStaffComplete.ColourRowsOfComboBox(Control: TWinControl; Index: 
Integer; Rect: TRect; State: TOwnerDrawState);
var
  myBkgColor: TColor;
  myFontColor: TColor;
begin
  if (State * [odSelected, odHotLight]) <> [] then
  begin
    myBkgColor := clHighlight;
    myFontColor := clHighlightText;
  end else
  begin
    case Index of
      0: begin
        myBkgColor := clWhite;
        myFontColor := clBlack;
      end;
      1: begin
        myBkgColor := Frm_Options.GetGradeColour1;
        myFontColor := ...;
      end;
      2: begin
        myBkgColor := Frm_Options.GetGradeColour2;
        myFontColor := ...;
      end;
      3: begin
        myBkgColor := Frm_Options.GetGradeColour3;
        myFontColor := ...;
      end;
      4: begin
        myBkgColor := Frm_Options.GetGradeColour4;
        myFontColor := ...;
      end;
    end;
  end;
  with (Control as TComboBox).Canvas do
  begin
    Brush.Style := bsSolid;
    Brush.Color := myBkgColor;
    Font.Color := myFontColor;
    FillRect(Rect);
    Brush.Style := bsClear;
    if Index = 0 then
      TextRect(Rect, Rect.Left, Rect.Top, (Control as TComboBox).Items[Index])
    else
      TextRect(Rect, Rect.Left+((Rect.Right - Rect.Left) div 2), Rect.Top, 
(Control as TComboBox).Items[Index]);
  end;
end;
{code}

--
Remy Lebeau (TeamB)
0
Remy
5/30/2012 6:52:17 PM
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}

> As Peter said, you need to look at the State parameter of the OnDrawItem 
> event to know how to draw each item.  Try something like this:


Its true I was ignoring the state variable as Peter said. I wrote that code from a snippet posted as a reply to a similar question and had no idea what the state variable did. Thank you Remy for making it clear. My combo box is now all singing and dancing!
Howard
0
howard
5/31/2012 12:09:24 PM
Reply:

Similar Artilces:

How do i get the font color of a TGroupBox when using vcl styles in XE7? [Edit]
I'm writing a Win32 app in XE7 using the VCL Luna Style. Sometimes a group box will need its caption color changed to alert the user that the data in the group box is required. So it's easy enough to set the StyleElements to [seBorder,seClient]. That allows me to change the font color and style. But then I want to change it back to the original color, the same color as other group box captions that are set to [seFont,seBorder,seClient]. So how do I get the font color of a TGroupBox when using VCL styles? This is what I've tried: {code} procedure TfrmMain.FormCreate(Sender: T...

How to change tree view's (web control) back color, font color and font size using CSS (Cascading Style Shteets)
Hi All, I have created tree view (web control) but i want to change all the properties of it, through single style sheet. Like all other controls (label, text box and combo box) it's not possible to change all the properties of treeview through single style sheet by including it to the contol ???? Please help........ MAITRAY. Sorry. No external css. but these attributes: DefaultStyle="background:#CCCCCC;border:solid 1px;color:black;font-size:8pt" HoverStyle="color:blue;font-name:Arial" SelectedStyle="c...

Signature font and color changes to message font and color
My first post. When I compose a message no matter what the signature font and color is set to, it changes to whatever the font and color is of the message. Is there a fix for this? I would ask my job's tech support but I wouldn't trust them to sit the right way on a toilet seat. -- doover ------------------------------------------------------------------------ doover, It appears that in the past few days you have not received a response to your posting. That concerns us, and has triggered this automated reply. Has your problem been resolved? If not, ...

Color changes AND font changes...
I would REALLY like to change the fonts for calendars just like I do with BG colors. Can I do it by adding something like font-size: 15pt !important in the calendar ICS file? Thanx, zeddock PS. I notice many patches are referenced here. Why is there not a more regular Sunbird release, nightly or weekly? zeddock wrote: > I would REALLY like to change the fonts for calendars just like I do > with BG colors. Can I do it by adding something like > font-size: 15pt !important > in the calendar ICS file? > > Thanx, > zeddock > > PS. I notice ...

Changing the font size and color of a Combobox [Edit]
I want to be able to change the font size and font color of a combobox at design time. No matter what I do, nothing works. Is this even possible without writing code? I would really appreciate help with the specific problem, but I'm also open to some good resources for learning to use Firemonkey styles. Thank You Edited by: Joey Hickey on May 6, 2013 12:47 PM Edited by: Joey Hickey on May 6, 2013 12:59 PM > {quote:title=Joey Hickey wrote:}{quote} > I want to be able to change the font size and font color of a combobox at design time. > > No matter what I do...

VCL styles in Delphi XE2
Hi, I've tried to prepare simple application with TButton and TPanel; procedure TForm1.btnMakeRedClick(Sender: TObject); begin pnlMain.Color := clRed; end; It works when i do not choose any VCL styles in the project settings; If i tried to use any of additional themes i can't change TPanel's color. We have large project with the same code logic, can i use vcl styles and keep color change functionality at the same time? Thank you, Alex. Hi Alex, We have also come across this issue. I submitted a QC Report - however they believe this is "As Designed"...

Using a GridView: how to change the font color depending on the content???
I am working on a web application using VB.NET 2005. I am filling some gridviews using data sets; and I was trying to change the font color for some records considering one of the fields content "text".for example: I would like to change to color red the whole row when the field "Status".Text = "Out of order"  the rest of the rows will remain with the same default color.thanks for suggestions. Something like this will work:http://weblogs.asp.net/hpreishuber/archive/2006/01/09/434889.aspxDarrell Norton, MVPDarrell Norton's BlogPlease mark this post as answered if it helped you!...

Using buglist.css to change font color by platform?
I'm trying to color code bugs based on platform on reports and assume I should be doing this via css/buglist.css. I see that currently font information is defined for severity field in this location. What would I need to add to alter based on the platform field? I'm running Bugzilla 2.16.2 on RedHat 8.0 Thanks in advance for your help! Aaron Everett Aaron Everett wrote: > I'm trying to color code bugs based on platform on reports and assume I > should be doing this via css/buglist.css. I see that currently font > information is defined for severity f...

Help: Change the Font Color of EditCommandColumn Edit, Update, Cancel buttons?
Hi, I'm trying to find a method for changing the the font color and sizes for the update, cancel, delete buttons of the EditCommandColumn of the DataGrid after clicking on the edit button. My ButtonType for the EditCommandColumn is Button. I was able to change the color of the the Edit button and the Delete button (Delete button prior to clicking on the Edit Button) by using an external CSS file and using the OnItemCreated="DataGridOnItemCreated". Under my fucntion, DataGridOnItemCreated, I accessed the two buttons using protected void DataGridOnItemCreated(object sender, ...

Delphi XE5 "font color" TButton customization on iOS7 [Edit]
On delphi xe5 1) How can I change the font color on a TButton on iOS7 (iPad) default style? 2) How can I export and modify all the default style iOS7 (iPad) ? Thanks for any feedback. Edited by: Giuseppe Amato on Feb 25, 2014 9:18 AM > {quote:title=Giuseppe Amato wrote:}{quote} > On delphi xe5 > 1) How can I change the font color on a TButton on iOS7 (iPad) default style? > 2) How can I export and modify all the default style iOS7 (iPad) ? > Thanks for any feedback. Chris Rolliston discussed how to get the system styles recently on his blog: http://delphihaven.w...

How to change the font color of an asp.net textbox control using JavaScript function?
How can I change the font color of an asp.net textbox control at run time using JavaScript function? Thanks. aspx code... <asp:textbox runat=server id="txtbox" text="hello world" /> <button runat=server id="mybutton">change color</button> aspx.vb code... mybutton.attributes("onclick") = "javascript:document.all." & txtbox.clientid & ".style.color='crimson';" while (!asleep()) ++sheep; Thanks for the answer. The problem is how to see this new font color in the server code after...

How to change the highlighted color of the selected item using user defined color instead of window system color
Thx, JJ You'd have to create your own "selection" system... you can use a rectangle object and change its visibility as needed to simulate a highlighted row, but you'd have to have (and use) your own versions of SelectRow() and its kin. "JJ" <jasonyklau@sinaman.com> wrote in message news:41205c89@forums-1-dub... > Thx, > JJ > > Why do your own selectRow()? If I'm not wrong, coding this is the visible property should work: "if(getRow()=selectedRow(),1,0)" hth, JMR > You'd have to create your own...

Third party Delphi component change IDE's font to 'system' typeface [Edit]
Hi I tried to use a third party component http://code.google.com/p/delph-sciter/ after I registered the component and tried to put it onto a form. most part of delphi IDE's font changed to 'system' typeface. I searched over web. looks like some person has the same issue before: http://groups.google.com/group/borland.public.delphi.ide/browse_thread/thread/71849ab73ec37ed2/e4c0cbac0487e919?hl=en&q=delphi+IDE+font&pli=1 Could anyone help this issue? Edited by: John Liuo on Apr 30, 2010 5:45 PM John Liuo wrote: > Hi > I tried to use a third party compo...

FormView control
When a user modifies the value in a textbox in a "EditTemplate" of the Form View, I would like the font color or back color of the text box to change - even before the update is submitted. How can I do that? Thanks. You can add an onchange event to your textbox: onchange="this.style.backgroundColor='yellow';" Or use the onkeyup event as well. You may need to add the handler for this if your textbox box is being created in the client side.Please mark as "answer" if this helped you in anyway. Thanks. here is how I did it:Protected Sub FirstName...

Web resources about - Change font color in a TComboxBox? [Edit] - embarcadero.delphi.vcl.using

Resources last updated: 2/13/2016 7:09:53 AM