Binding list problem with listbox



I have created a Component called a BOConnector that implements IBindingList
so it can provide access to a list of business objects to be bound to

At design time there is no live list of objects so BOConnector creates an
internal list of one object. It needs one object because when you bind to a
grid etc at design time it needs to get a list of properties to make the

My BOConnector works just fine with grids and simple controls like edit
boxes but I have a problem with the ListBox and ComboBox.

When I connect the BOConnector to the ListBox.DataSource it seems to create
a copy of the internal business object and try to save it into its
ListBox.Items in the form definition. I seem to see what is going on here,
it is taking a copy of the data and loading it into its Items. But why is
it doing this at design time? And the complex nature of my business object
means this messes up the form totally.

My first thought is to create a descendant of the ListBox and ComboBox and
make sure it does not copy items from the DataSource to the Items list at
design time.

Has anyone got any ideas?



Kevin Spencer

Hi Andrew,

Yes, it's possible to prevent certain code from being executed at
Design-time. The reason for this is that the Controls need to be painted at
Design time, and some logic may be involved in the process. This is a very
simple explanation of a fairly complex topic, so I'll finish with a link to
the full reference for you.

There are certain methods that automatically execute at Design-time, such as
Constructors, InitializeComponent, and OnLoad. There are also Designers that
are involved in the Design-time code execution, and attributes as well.
Fortunately, all of this is available to us, the developers.

There is one easy way to prevent code from executing at Design time,
however. Enclose it in an "if" block:

if (!DesignMode)

Fore more details and all kinds of stuff you can do with Designer classes
and attributes, see:


Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.




Thanks for your answer Kevin, but I know all about DesignMode etc. My
question was really about why these controls are behaving is such a way with
data binding and is the only solution override this behaviour.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question