Recent

Author Topic: [WISH] Easier TComboBox color  (Read 1525 times)

Xenno

  • Jr. Member
  • **
  • Posts: 60
    • BS Programs
[WISH] Easier TComboBox color
« on: January 03, 2026, 12:43:27 pm »
Changing color of TComboBox seems too much to do. Since D7, setting a TComboBox color is simply changing its Color properties. The Color will also applied to the drop down list.

I try to get along with Lazarus by setting TComboBox's style to csOwnerDrawFixed and supply code in OnDrawItem event. One thing that I cannot solve is the TComboBox height which shorter than a regular one. AutoSize does not help.

I wish in 2026 Lazarus will allow changing color of a TComboBox easier without any side effect.
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

wp

  • Hero Member
  • *****
  • Posts: 13397
Re: [WISH] Easier TComboBox color
« Reply #1 on: January 03, 2026, 03:17:38 pm »
I try to get along with Lazarus by setting TComboBox's style to csOwnerDrawFixed and supply code in OnDrawItem event. One thing that I cannot solve is the TComboBox height which shorter than a regular one.
The height of a Combobox having a csOwnerDraw* style is controlled by the ItemHeight property

Xenno

  • Jr. Member
  • **
  • Posts: 60
    • BS Programs
Re: [WISH] Easier TComboBox color
« Reply #2 on: January 03, 2026, 04:39:23 pm »
Thank you, wp. Yes, ItemHeight adjusts the height but don't we need to calculate correct ItemHeight value for any font settings? I tried ItemHeight and AutoSize properties and I dared myself to examined customcombobox.inc.

There should be a complex reason why so much to do in order to set a TComboBox and its drop down list to have same custom color.
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

wp

  • Hero Member
  • *****
  • Posts: 13397
Re: [WISH] Easier TComboBox color
« Reply #3 on: January 03, 2026, 06:19:15 pm »
There are two aspects in your question:
(1) Getting the custom color into the combobox
(2) Adjusting the combobox height with the font size

ad (1): I switched the Combobox.Style to csOwnerDrawEditableFixed, or csOwnerDrawEditableVariable, csOwnerDrawFixed or csOwnerDrawVariable, set the ComboBox.Color to clYellow - and the control as well as the dropdown are yellow, without any additional code. This is on Windows; maybe it is different on other widgetsets.

ad (2): When doing these changes of ComboBox.Style and increase the ComboBox.Font.Size, the height of the control remains constant. To call the following procedure from the form's OnCreate event; it simply measures the height of the text:
Code: Pascal  [Select][+][-]
  1. procedure AdjustComboboxHeight(AComboBox: TComboBox);
  2. var
  3.   canv: TControlCanvas;
  4. begin
  5.   canv := TControlCanvas.Create;
  6.   try
  7.     canv.Control := AComboBox;
  8.     canv.Font.Assign(ACombobox.Font);
  9.     AComboBox.ItemHeight := canv.TextHeight('Tg');
  10.   finally
  11.     canv.Free;
  12.   end;
  13. end;


« Last Edit: January 03, 2026, 07:56:52 pm by wp »

Xenno

  • Jr. Member
  • **
  • Posts: 60
    • BS Programs
Re: [WISH] Easier TComboBox color
« Reply #4 on: January 03, 2026, 09:36:45 pm »
Thank you, wp. Yes, those aspects are related. Changing color impacts height. I understand we can (simply) calculate ItemHeight using canvas but the case of TComboBox I think it needs magic formula which increased by 2. Please, see attachments. Maybe 2 is not always correct.

Code: Pascal  [Select][+][-]
  1. MyComboBox.Color := clYellow;
  2. AdjustComboboxHeight(MyComboBox);
  3. MyComboBox.ItemHeight := MyComboBox.ItemHeight + 2;
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

wp

  • Hero Member
  • *****
  • Posts: 13397
Re: [WISH] Easier TComboBox color
« Reply #5 on: January 03, 2026, 11:06:57 pm »
Yes I agree: this is unnecessarily complicated, ComboBox.Height is not changeable directly, even when AutoSize is off, and even when top and bottom sides of the combobox are anchored to the same sides of a TEdit, the TCombobox cannot be forced to get the same height as the Edit. But in essence, Delphi has the same non-sense, probably it originates in Windows?

Your observation with the delta of 2 seems to be correct: I tested several Font.Sizes (up to 40pts) and Font.Names, and in every case the AdjustComboboxHeight with added 2 is able to get the ComboBox the same height as a TEdit with the same fontsize.

Code: Pascal  [Select][+][-]
  1.     procedure AdjustComboboxHeight(AComboBox: TComboBox);
  2.     var
  3.       canv: TControlCanvas;
  4.     begin
  5.       canv := TControlCanvas.Create;
  6.       try
  7.         canv.Control := AComboBox;
  8.         canv.Font.Assign(ACombobox.Font);
  9.         AComboBox.ItemHeight := canv.TextHeight('Tg') + 2;  // 2 needed to get the same height as a TEdit
  10.       finally
  11.         canv.Free;
  12.       end;
  13.     end;

jamie

  • Hero Member
  • *****
  • Posts: 7587
Re: [WISH] Easier TComboBox color
« Reply #6 on: January 04, 2026, 01:43:17 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
  2.   var AHeight: Integer);
  3. begin
  4.   Aheight := Abs(Control.Font.Height);
  5. end;                            
  6.  
  7.  

Using the FONT height seems to work for me the best.

Jamie
The only true wisdom is knowing you know nothing

Xenno

  • Jr. Member
  • **
  • Posts: 60
    • BS Programs
Re: [WISH] Easier TComboBox color
« Reply #7 on: January 04, 2026, 07:03:08 am »
Yes I agree: this is unnecessarily complicated, ComboBox.Height is not changeable directly, even when AutoSize is off, and even when top and bottom sides of the combobox are anchored to the same sides of a TEdit, the TCombobox cannot be forced to get the same height as the Edit. But in essence, Delphi has the same non-sense, probably it originates in Windows?

Thank you for the confirmation, wp. It may cause issues in layout consitency and aesthetic. Actually Delphi does not this issue as I mentioned in first post. Attached D7 without XPManifest, D7 with XPManifest, and D12. No need to alter ItemHeight nor change Style; just as I mean "easier". So, I guess the issue is not originated from Windows. Or, did you mean another nonsense?

Using the FONT height seems to work for me the best.

Thank you, jamie. But from that event, we can't adjust combobox's height which is still needed.
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

jamie

  • Hero Member
  • *****
  • Posts: 7587
Re: [WISH] Easier TComboBox color
« Reply #8 on: January 04, 2026, 02:34:33 pm »
I just experimented with ItemHeight when the combo is in OwnerFixed mode via the IO by setting the ItemHeight property to whatever and it works as expected, it sizes the combo to your request

Also, I did it without using any owner draw modes and with that, you need to set the FONT.Height or Size, preferably the Height of the font and that will set the size of the combo too!.

 In Windows, normally setting the font size dictates many of the native control sizes unless you go into owner mode, then you must supply something.


Jamie
The only true wisdom is knowing you know nothing

Xenno

  • Jr. Member
  • **
  • Posts: 60
    • BS Programs
Re: [WISH] Easier TComboBox color
« Reply #9 on: January 04, 2026, 03:42:45 pm »
I just experimented with ItemHeight when the combo is in OwnerFixed mode via the IO by setting the ItemHeight property to whatever and it works as expected, it sizes the combo to your request

Also, I did it without using any owner draw modes and with that, you need to set the FONT.Height or Size, preferably the Height of the font and that will set the size of the combo too!.

 In Windows, normally setting the font size dictates many of the native control sizes unless you go into owner mode, then you must supply something.


Jamie

You are very right, jamie. Thank you. But, as seen my first post, the drop down list should have same color. Therefore the Style needs to be owner draw to let us colorize in OnDrawItem. The owner draw style triggers height issue. Apology to compare again, in D7, changing Color property of a TComboBox will also makes its drop down list has the same color.

Yes, it is a small thing. I need it to have my combo boxes show black drop down list when my app in dark mode. That's the wish... easier.
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

jamie

  • Hero Member
  • *****
  • Posts: 7587
Re: [WISH] Easier TComboBox color
« Reply #10 on: January 04, 2026, 05:16:54 pm »
I think part of the problem is that when D7 was out a lower version of the ComCtrl32 was being used and as far as I know, MS still installs these for older apps. There is a subtitle difference between how the current 6.xx behaves.

 If you were to compile your app in D7 and move it to a new machine in EXE, it most likely will give you what you expected however, if you recompile with newer tools, changes are a newer ComCtl32 could be used and thus things like Themes etc..


 There is a hack that can be done and that is you get the combo info via GetComboBoxInfo which returns a structure that contains all the needed items like RECT plots for components, the EDIT box handle and ListBox handle.

 You can hook the Window Procedure of the ListBox and thus track the WM_Paint message where you can at that point set the colors to your liking.

 But really, after all of this, it's simpler to use the owner draw, the only drawback is it's not supported with the csSimple style which I like to use so I can see a combination of edit control and listbox at all times.

Jamie


The only true wisdom is knowing you know nothing

Xenno

  • Jr. Member
  • **
  • Posts: 60
    • BS Programs
Re: [WISH] Easier TComboBox color
« Reply #11 on: January 04, 2026, 05:49:57 pm »
Yes, jamie. Thank you.

The solution is already found: owner draw, ItemHeight + 2, and OnDrawItem.
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

wp

  • Hero Member
  • *****
  • Posts: 13397
Re: [WISH] Easier TComboBox color
« Reply #12 on: January 05, 2026, 12:43:55 am »
Or, did you mean another nonsense?
I mean the "feature" to link a "primary" property of all controls (Height) to a "secondary" property of this specific class (ItemHeight) so that Height cannot be selected independently of ItemHeight. The combobox behaves as if AutoSize were active all the time, even when the property is turned off. OK - Delphi is less confusing in the sense that its TComboBox does not have an AutoSize property at all and it can be said: Well, it always has AutoSize=true.

A comment in customcombobox.inc indicates that this behaviour may be intentional: "AutoSize must be true by default to provide good cross-platform development experience as some widgetsets (win32, wince) ignore the combobox height and others (gtk2) look ugly with a too small height"

As for the offset 2, I spent some time today in finding where ItemHeight gets its default value, but I was not successful. The idea was that adding 2 to the value obtained in this method should fix the height difference for ownerdrawn styles to have the same behaviour as in Delphi.

Maybe you should file a bugreport at least about the Style-dependent Height inconsistency. Add you test project to the report.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12170
  • Debugger - SynEdit - and more
    • wiki
Re: [WISH] Easier TComboBox color
« Reply #13 on: January 05, 2026, 01:04:47 am »
Well, I must say - following this - I was surprised myself, rather much.

ItemSize (when ownerdrawn) overrides all other size-settings. The Height, any anchors to other controls (anchorsides), even Constraints.MinHeight - really, a property stronger than constraints?

At least from my tests on Windows...

Now I checked the Delphi docs:
https://docwiki.embarcadero.com/Libraries/Florence/en/Vcl.StdCtrls.TCustomCombo.ItemHeight
Quote
Specifies the height, in pixels, of the items in the drop-down list.

So the actual closed "box" is not affected according to that.

That is much more like what I expected.

wp

  • Hero Member
  • *****
  • Posts: 13397
Re: [WISH] Easier TComboBox color
« Reply #14 on: January 05, 2026, 01:51:13 am »
a property stronger than constraints?
Yes

Now I checked the Delphi docs:
https://docwiki.embarcadero.com/Libraries/Florence/en/Vcl.StdCtrls.TCustomCombo.ItemHeight
Quote
Specifies the height, in pixels, of the items in the drop-down list.

So the actual closed "box" is not affected according to that.

That is much more like what I expected.
Docs is misleading here: The Height cannot be entered directly, it is derived from the ItemHeight, like in the LCL. It does react on changes of Constraints.MinHeight, though.

 

TinyPortal © 2005-2018