Link to home
Start Free TrialLog in
Avatar of tonydm
tonydm

asked on

Multiple ComboBoxes reference the exact same list. Is there a better way?

Hi,

I've got a question regarding the best approach to a problem.  I have an application that requires a selection box providing a list of items.  I need that same list of items in twenty other locations in the program.  Is there a better way than to populate those 20 comboboxes with the same stringlist derived from a table?  The problem also, is that each of these comboboxes need to retain the selection made.  If I use a dbcombo, the selection does not remain when another dbcombo makes a selection on the same table.  I've tried dblookupcomboboxes as well.   I also am concerned with the memory needed to have twenty comboboxes all with the same data to list.   Any suggestions are welcome and appreciated.

TD
ASKER CERTIFIED SOLUTION
Avatar of DeerBear
DeerBear

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are you actually re populating the boxes or are you doing the following :-

  Combobox2.Items:=Combobox1.Items;
  Listbox1.Items:=Combobox1.Items;
  AnyDelphiList.Items:=Combobox1.Items;
Avatar of Robn
Robn

tonydm,
No, not really. If you want to use the VCL, there is no way to solve your problem.
actually there could be a lot of ways to solve this problem, but the best solution will depend on case specific details which are not given at this moment.

regards
classmate.

If you're using the VCL, no there are not. However, you can easily create your own combo component with your own TStrings implementation that could be shared. You would need to manage the combo boxes so they can react to changes in the TStrings class. But this is easy to do.
If you're worried about memory resources, Delphi automatically does reference counting on strings. If you had the value "Hello World" in 20 combo boxes, Delphi would only allocate one string and share it out. The OS would still have 20 character arrays of "Hello World" though. This cannot be helped.
However, if you want something easier than managing 20 combo box items, you'll need to manage this yourself and create your custom component.
tonydm,

Are you saying that you need 20 independent selections?  Or changing one changes all?  Is the users selection stored in the database? (in one field or twenty different fields?)

Your memory budget must be tight if you're worried about 20 short stringlists.  But if so, create them on-the-fly as needed, then free them afterwards.  Load a TStringsList with the items then assign it to the Items property of the combo:  ComboBox1.Items.Assign(MyStringList);
Avatar of tonydm

ASKER

wow!  thanks for all the rapid feedback.  Let me try and be a little more clear and answer as many ?'s as possible.  I'm new to Delphi, a novice, if you will.   My concern over memory is that my program is rather small, but has a compiled exe of a bit over 2mb.  That's will optimization, and no map or debug code.  My stringlist has about 300 1-3 digit strings.  So i was concerned about the impact that 20 comboboxes each containing the same information.  I am reading this information in from a table.  The selected item is then the key to seek on in the same table the list read for two other pieces of information.  This is then written to a temp file used will the app is running.  So in other words.  The user can select from multiple dropdowns, 20 of which provide the same information to pick from. I know, it sounds crazy, but it makes sense.  It is a kind of a schedule that the user is setting up over the course of a month.  There are a few other comboboxes with name, addr, etc.  matched with  I originally tried dbcomboboxes as well as blookupcomboboxes.  But in each case, whenever I would make a selection, the record pointer would change thereby reflecting in the other dbcomboboxes.  And something else that seemed strange to me was that I would still have to populate the dbcomboboxes with the same stringlist read from the table in which the dbcomboboxes were to derive there list.  I thought I had to missing some really simple.  But if I tried the same with a third party dbaware dbcombobox, I didn't have to populate that component with a stringlist.

The question was asked how I am populating the comboboxes.  The follow way:
Combobox1.Items.Assign(MyStringList);
Combobox2.Items.Assign(MyStringList), and so on...

And the dbcombo in the same manor.  The dblookupcombo provided the list without having to first populate the a strings property.  But as stated, move one db or dblookup and they all move.

I am using the vcl, in that I have populated number of tabsheets on a single form.  If this can be don't at runtime and then have at runtime that could be good.  If it really provides some benefit.

DeerBear, please, I'm looking at your code and with some study, I'm sure it will make sense, but could you explain in just a bit more detail?  Thank you, thank you.

TD
Hi,

My code represents a singleton list, which holds a string list.
If you place that code in a unit( File|New...|Unit with D5+), you'll be able to
reference the EXACT SAME list from the whole application.

You can then assign them through the ComboBox1.AddStrings( SingleList.List ); and so on.
This will make you reference all the time from the same items.

HTH,

Andrew
DeerBear's code will work perfectly for you if you were using 1 list for every combo box, but looking at your code sample....
Combobox1.Items.Assign(MyStringList);
Combobox2.Items.Assign(MyStringList), and so on...
it looks to me like you populate MyStringList with all the values and use this to populate the combo boxes which is essentially the same.

Unless you wanted to create a custom combo box that had the ability to share TStrings with other combo boxes (which to me seems like overkill) I would leave your code the way it is.

As I posted before, Delphi Strings are reference counted. Delphi has one of the most advanced String class I've seen in a development language.

There is nothing wrong with trying to tweek your code by writing the custom component, but you'll be spending a lot of time debugging it and I don't believe the gain will be worth the effort.

My $0.02

Regards,
Rob
tonydm,

it sounds to me like you have a table of codes like this:

101  Wrench         2.50
102  Screwdriver   1.75
103  Hammer        3.50

And you want the user to select a code in 20 different places.  Then, you would probably be better off using 20 TDBLookupComboBox controls.  The trick here, though, is each one needs a different target DataField property.  This will keep each selection independent.  You will have to create a record in another table to hold the values.  (You mentioned earlier that this was a temporay file)  The DataSource should point to this new table.  The ListSource points to the table of codes, above.

You could create and populate the combobox dyamically before the form is shown and destoy them when the form is closed. This way you would not be taking up any significant memory. The code to create / populate/ destroy the combobox could be held in a single unit you call.