I have a very huge number of string to be loaded to MFC Combo Box. To set the width of my combo box, i am using GetTextExtent for every string and setting the maximum extent on my combo box. This is very time consuming as the call GetTextExtent takes lot of time on whole. While adding 25000 strings this is getting very expensive. Is there any other way i can get the exact width that i can set on my combo box ?
-
I tried Graphics.MesasureString and it was also taking the same considerable amount of time. Is there any other way ? – user273121 Mar 14 '16 at 12:12
-
Question: I'm assuming you're concerned about the width of the string in the **edit** portion of the combo box once a selection has been made. Is your concern that the string would be truncated if the width of the combo box is not sufficient? – rrirower Mar 14 '16 at 15:39
-
At 25000 elements - I would cheat. GetTextExtent on an average character - like 'a' and multiply it by the number of characters in the longest string length. – Jason De Arte Mar 14 '16 at 16:50
-
1I am concerned about the width of the dropdown box for the combobox. I do have a SetDropdownWidth but which value to set is what is the issue here. – user273121 Mar 14 '16 at 17:06
-
Is there any way to set resize option to the dropdown list of the combo box ? – user273121 Mar 15 '16 at 11:37
-
1May I question your design? What do you expect the users of that control to do? Page around those 25,000 strings to see what's there? There has to be a better choice for finding a string in a large set. – Vlad Feinstein Mar 15 '16 at 13:41
2 Answers
Method 1: Find worst case requirement
Starting with: Which letter of the English alphabet takes up most pixels?
It appears character W
is the widest character. (Or use a loop to find the widest character) You can check the length of largest string and make a string of same length with W
filled for all character. Now width of this string gives the worst case requirement for the combobox if it works for you.
Method 2: Save some calls
Find the ratio of widest and least wide character. Now find the length of largest string. Now do second iteration and find the width of string only of the length of string is more than largest_length * ratio
. This will save some calls.

- 1
- 1

- 30,259
- 8
- 73
- 100
-
I used first method and my client is not happy with the UI. Can you explain me how Method 2 works ? – user273121 Mar 14 '16 at 12:20
-
Let's say widest character is `W` with width 24 and least wide character is `i` width width 12. Now let's say length of largest string is `50`. Now even if this largest string is made of all `i`s, it would take more space than all the string of size `25` or more. Even if they are made of all `W`s. That means you can skip checking text width of all the strings with length 25 and less. (`max_len * least_wide_char / widest_char`). – Mohit Jain Mar 14 '16 at 12:24
-
Is there any way to set resize option to the dropdown list of the combo box ? – user273121 Mar 15 '16 at 11:37
You can dynamically update width of your ComboBox when too long string is about to be shown. If you have 25000 positions to show then soonner or later you will have twice that. Another hint is to use Virtual ListCtrl - but that only if adding all those strings to your combobox is too slow.

- 48,511
- 9
- 79
- 100
-
Is there any way to set resize option to the dropdown list of the combo box ? – user273121 Mar 15 '16 at 11:37
-
@user273121 I am not sure if I understood, but see this article: https://weseetips.wordpress.com/tag/adjust-combobox-dropdown-width/, you should use CComboBox::SetDroppedWidth – marcinj Mar 15 '16 at 11:58
-
Hi, I'm already using this option. But to set the width of the dropdwon box, for performance issue i dont want to use GetTextExtent() method. So if i set an approximate width for my dropdown box, i want to have resizable gripper with which customer can extend the dropdown box to desired size just like we resize the windows. – user273121 Mar 16 '16 at 09:20