views:

3110

answers:

6

Hi Everyone,

I have a fairly complex and large application that hands loads and loads of data. Is there a speedy way to add items to ComboBox that doesn't take so long? On my P3 3.2ghz, the following snippet takes just under a second to add around 32,000 items. (MasterCIList is a StringList with strings typically 20 - 30 bytes long).

with LookupComboBox do
 begin
  Items.BeginUpdate;
  Items.Clear;
  for i := 0 to MasterCIList.Count - 1 do
   Items.Add(MasterCIList[i]);
  Items.EndUpdate;
 end;

Drilling down into the VCL, it appears that in TComboBoxStrings.Add, there is a call to Result := SendMessage(ComboBox.Handle, CB_ADDSTRING, 0, Longint(PChar(S)));

I'm guessing this is really taking up time (okay, I know it is). Is there another way to populate the Items that is speedier? Any high speed combox boxes available? I have the TMS components but they seem to be extensions of TComboBox.

For instance, I have the PlusMemo which seems to be a total rewrite of a TMemo. I can easily add a million line in a second to a PlusMemo. A TMemo, I don't think so.

Thanks so much for your time!

+1  A: 

perhaps cmb.Items.Assign(myStringList) will help.

here's a wild idea: i haven't tried it but you might check to see if there's a way to virtually load the combobox by setting the number of items and then owner drawing. please pardon this crazy idea but i think i've heard of this being available somehow. irrelevant: this is how it's done in Palm OS...where the faster way to load the combobox is to not load it all... ;-)

Not an answer, but why on earth would you want 32,000 items in a combo box? That is a terrible way to store that much data.

i agree; it's a bad practice...

X-Ray
+13  A: 

Sorry if I'm a nuisance, but I doubt a TComboBox with 32,000 items is even remotely ''usable'' --- I'd say there's a reason why it's slow: it was never meant to do this :)

Would there be a possibility to filter the data, and only load a subset? To be more concrete, in one particular database application I've been working on, the user can search for a person. We let the user type at least 3 or 4 characters of the name, and only then begin to return results in a listbox. This has greatly increased usability of the search form, also greatly speeding up the whole process.

Would you be able to follow a similar approach?

Or, on a completely different take, perhaps you could take a look at the VirtualTreeView component --- either for direct use, or for inspiration.

onnodb
It's a good tip. Maybe add the strings in the OnDropDown handler, filtered to begin with the chars that are already entered. That way the user has influence on the delay. In any case, a lookup list with 32000 items is unusable, even if instantly populated.
mghie
+1  A: 

I agree that 32K items is a ridiculous amount to have in a combobox... That being said, you might try adding the items to a TStringList first and then use a combination of Begin/EndUpdate and AddStrings:

SL := TStringList.Create;
try
  // Add your items to the stringlist
  ComboBox.Items.BeginUpdate;
  try
    ComboBox.Items.AddStrings(YourStringList);
  finally
    ComboBox.Items.EndUpdate;
  end;
finally
  SL.Free;
end;

The code compiles, but I didn't test it further than that; I've never felt the need to add more than a few dozen items to a combobox or listbox. If any more items might be needed than that, I find a way to filter before populating the list so there are fewer items.

Just out of curiosity, how do you expect a user to sort through that many items to make a decision?

Ken White
A: 

Hi Everyone,

It's me again. I'm adding 32,000 items cause I need to. That's one of many controls in my application that has "lots" of items. I need to have that many items. It works just fine looking things up. Perfectly in fact. I'm just trying to optimize things. The users find things just fine since they are in a certain logical order.

Everything I've seem so far with Assign and AddStrings is that they eventually end up in Add with the SendMessage call. So I'll keep looking.

Thanks for the feedback.

First, posting an "answer" like this is wrong. Edit your original post. Second, again, I've been programming for 20+ years, Win apps for 15+, have done some extraordinarily complex apps, and have never found the need for even 1K items in a combobox. But good luck to you (and the users of your app).
Ken White
Agree with Ken. To be clear - it's fine to post an answer if it's really an answer to your question. If you have a comment then just edit your question.
Argalatyr
A: 

Hi Steve. Perhaps you can use a database engine in the back-end and use a data aware component. Then the things will be much more quicker and usable. Perhaps if you'll try to describe what do you try to accomplish we'll help you further. In any case, your UI design is, let's say, odd. And for this perhaps the Embarcadero forums will help you better.

A: 

i implement this in a different manner .first i removed the combobox control and take textbox control and assign it autocomplete to custom source where the custom source string collection is 32 k items.I get the selected value from a new query on controls validation. So it can replace combobox functionality.Mostly about 32K items people dont scroll but they keep entering key strokes and is catched by our custom auto complete source..

if any problem(s).. let me know....

By Vinod Babu R

Vinod Babu