Jump to content
Mark-

TComboBox color when not "dropped"...

Recommended Posts

Hello,

 

Delphi 10.2

 

image.png.7770253ab8f3dd44f5f49629a56bba51.png

 

Both TComboboxes have the same settings except for the style.

I have not found a method at design time or run time, to make the box with csDropDownList style, correctly apply the color property of the control.

Perhaps I am not seeing the forest for the trees.

 

Ideas?

 

Thanks,

 

Mark

 

 

 

 

Share this post


Link to post

At design-time, setting the Color property will work for csDropDownList, but at runtime that doesn't work.  So instead, you have to set the Style property to csOwnerDraw... and use the OnDrawItem event to manually paint the individual list items however you want, including the ComboBox's edit area above the list, eg:

procedure TMyForm.ComboBox2DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  ComboBox2.Canvas.Brush.Color := ...;
  ComboBox2.Canvas.Font.Color := ...;
  ComboBox2.Canvas.FillRect(Rect);
  ComboBox2.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, ComboBox2.Items[Index]);
end;

If you want to paint the edit area differently then the list items, you can differentiate by checking if the odComboBoxEdit flag is present in the State parameter, eg:

procedure TMyForm.ComboBox2DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  if odComboBoxEdit in State then
  begin
    // edit area
    ComboBox2.Canvas.Brush.Color := ...;
    ComboBox2.Canvas.Font.Color := ...;
  end else
  begin
    // list item
    ComboBox2.Canvas.Brush.Color := ...;
    ComboBox2.Canvas.Font.Color := ...;
  end;
  ComboBox2.Canvas.FillRect(Rect);
  ComboBox2.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, ComboBox2.Items[Index]);
end;

 

Edited by Remy Lebeau

Share this post


Link to post

Thanks Remy.

 

I have done that for other combos, like pen width. 

Found it odd it worked out of the box for csDropDown but, not for csDropDownList.

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×