Jump to content
dummzeuch

Setting the drop down width of a Combobox in Delphi

Recommended Posts

By default, the width of the drop down list of a TComboBox is the same as the width of the control itself, and even in the latest Delphi version there apparently is no property to set it.

combobox-normal.png.43bb050c66db02a19aa72f1762259df3.png

Why is that so? Good question. There are probably many third party controls that offer this because it is rather simple to implement. But on the other hand, if it is that simple, why isn’t it a feature of the default control? It can really be a pain in the lower back that some entries are just not displayed correctly as seen in the picture above.

Setting the drop down width is as simple as sending the CB_SETDROPPEDWIDTH message to the control’s handle:

1 SendMessage(TheCombobox.Handle, CB_SETDROPPEDWIDTH, MinimumWidthInPixels, 0);

combobox-250.png.53f74b0fec62d0bfba06bf5c64909c29.png

It does not allow to shrink the width of the drop down list though, because it sets the minimum width, not the actual width. There is this answer on StackOverflow for that particular problem. The result isn’t very visually appealing though, because the list is left aligned rather than right.

combobox-50.png.e00676301fd353fd423ef1b391f789f2.png

 

Read on in my blog post

https://blog.dummzeuch.de/2019/06/22/setting-the-drop-down-width-of-a-combobox-in-delphi/

Edited by dummzeuch
pictures didn't work
  • Like 2
  • Thanks 1

Share this post


Link to post

In most cases the widened list is of only limited benefit when the selected item doesn't fit into the edit field either.

  • Like 1

Share this post


Link to post
50 minutes ago, Attila Kovacs said:

because VCL is abandoned

hm. No. .. ? Why should it be?

  • Like 1

Share this post


Link to post

In Delphi 11.2 (maybe also in earlier versions), I have noticed that there now is the property "AutoDropDownWidth" on "TComboBox" to do this.

  • Like 1
  • Thanks 1

Share this post


Link to post

In Delphi 11.2, you can change the ComboBox dropdown width using:  .DropDownWidth := width, but it is also in the Object Inspector window. 

 

And, .AutoDropDownWidth is a boolean, and also in the Object Inspector window.  If true, then the width of the cbx will be whatever is the largest text inside the dropdown. 

When set to false (which is its default), the standard default width is used. 

  • Thanks 1

Share this post


Link to post

What's especially awesome about the VCL code is that it writes the for loop in a really weird way as 

for var I: Integer := 0 to -1 + Items.Count do

with the -1 before the count, and that seems to have taken from Setting the drop down width of a Combobox in Delphi – twm's blog (dummzeuch.de) which in turn is taken from Sizing the ComboBox Drop Down Width (thoughtco.com)

Share this post


Link to post
22 minutes ago, David Heffernan said:

What's especially awesome about the VCL code is that it writes the for loop in a really weird way as 


for var I: Integer := 0 to -1 + Items.Count do

with the -1 before the count, and that seems to have taken from Setting the drop down width of a Combobox in Delphi – twm's blog (dummzeuch.de) which in turn is taken from Sizing the ComboBox Drop Down Width (thoughtco.com)

THeY fouND tHe cOdE On ThE InTeRwebS. It muST be ThE beSTESt!

  • Haha 4

Share this post


Link to post

So, why is that a problem? Is the code wrong? Or is it just not the way you would have written it?

Share this post


Link to post

Not so fast - it more likely was some AI that learned it from those sources - soon all loops will be written that way!

5 minutes ago, dummzeuch said:

So, why is that a problem? Is the code wrong? Or is it just not the way you would have written it?

The problem is that apparently whoever implemented it that way in the VCL did not even care to apply their style guide to the code.

 

And I am not even going into the legal ramifications of Embarcadero copying code from the internet and then putting it under their copyright.

Edited by Stefan Glienke
  • Haha 1

Share this post


Link to post
6 hours ago, dummzeuch said:

So, why is that a problem? Is the code wrong? Or is it just not the way you would have written it?

I've never in my life seen it written like that. Sure it works. But when you read it, it's not familiar and so it makes you think about it. Impedence. Patterns matter. 

  • Like 2

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

×