By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,219 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,219 IT Pros & Developers. It's quick & easy.

Design Question: onChange Event populating a second list box

P: n/a
I'm looking for thoughts on the "correct" design for this problem (DotNet
2.0 - in winforms, but that's not so important). I've got two combo boxes
(combo1 and combo2), both are populating via database calls (using a
separate DB handler class). "combo1" contains a list of countries and is
fairly static, it can be added to but no other external events cause a
change in its population. "combo2" however is populated with a list of
options specific to a particular country. Changing the selected item in
"combo1" causes a refresh of the items listed in "combo2".

In my solution, I've extended the ComboBox for "combo2", allowing it to
contain a reference to both "combo1" and the DB handler class (they are
passed to, not created in the extended ComboBox class (note "combo1" has
also been extended to contain a ref to the DB handler class and set itself
up from it). This has allowed me to attach the code that populates "combo2"
to the OnSelectedIndexChanged event of "combo1". It produces really nice
tight code, but I've just run into an issue that makes me think that I may
not have the best design (it's close but not quite). Basically the issue is
how, if you persist the previous user selection, do you safely set up the
control(s) the second+ time that the user enters the form.

Pseudo-code of the issue:

combo1.DbHandler = dbh1; // Populates list with contents of dbh1
combo2.DbHandler = dbh2; // Assigns db2 to control, no list population
as no country selection yet.
combo2.CountryComboBox = combo1; // no have implied country selection,
and populates list accordingly
combo1.SelectedItem = UserSetting.cb1Value; // Changes selected
country, which fires updating of list in combo2

// I'm worried about this next line!
combo2.SelectedItem = UserSetting.cb2.Value; // Should set selected
option for previously selected country

I think in the above example, the line of code that tries to set the
selected item on combo2 could be run before, or at the same time as, the
event that updates combo2's list (triggered by setting the selected item in
combo1). Is this true, do triggered events run in parallel to the code that
triggered them? If so, what's the solution? Please note in the actual
form, I've got 4 copies of this user control and adding a country or option
for a country in one of them, needs to update all the others - this is why
the extended combobox contains a reference to the db handler. The Create
function in the dbhandler fires an event that the extended combobox attaches
to. This in turn causes the list to update accordingly. I feel that I'm
not far from a MVC design pattern, and it may be the answer - but I don't
want to just end up moving the same problem out into another class.

Any thoughts, ideas or assistance would be much appreciated,

- Paul.
Jan 24 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.