Reputation: 27
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 ?
Upvotes: 2
Views: 433
Reputation: 49976
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.
Upvotes: 1
Reputation: 30489
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.
Upvotes: 2