Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a large array of strings in to an MFC combobox control fast as possible?

I have an array of 1000 strings to load into a combo box. What is the fastest way to load the array of strings into the combo box?

Is there some way other than iterating over the list of strings, putting each string into the combo box one at a time?

And how to copy the combo box data once loaded to some 10 other combo boxes?

like image 307
yesraaj Avatar asked Oct 23 '08 11:10

yesraaj


3 Answers

If you have 1,000 strings repeated in 10 comboboxes, you may want to consider using an owner drawn combobox, which draws the strings on the fly based on indices into your array, rather than storing them in the combobox at all. Way faster, way more memory efficient. Check out the DrawItem method and DRAWITEMSTRUCT structure in the on-line help. Basically, you would do something like using InitStorage and InsertString (as mentioned by NuSonic) to create your 1000 blank items in your combobx, and override DrawItem to extract and draw the required string, based on index, as it needs to be drawn.

like image 161
SmacL Avatar answered Nov 15 '22 06:11

SmacL


I'm not aware of any way to load multiple strings atomically, but there are a couple of things that you can do to make the process more efficient:

  • Call CComboBox::InitStorage before adding items to pre-allocate memory
  • Use InsertString instead of AddString to prevent triggering a sort on each addition (assuming the CBS_SORT style is enabled)
like image 2
Stu Mackellar Avatar answered Nov 15 '22 07:11

Stu Mackellar


Possibly faster than CComboBox with DrawItem would be an owner data CListCtrl. This would also serve your goal of duplicating a subset of the items into other lists, since a common data source could be used.

I suggest this because a 1000-item CComboBox isn't going to be very usable.

like image 1
Aidan Ryan Avatar answered Nov 15 '22 07:11

Aidan Ryan