Dynamically Added Controls

Topics: CAB & Smart Client Software Factory
Jul 8, 2006 at 11:27 AM
originally posted by: TimLarsonEP

I am creating a query builder which allows the user to add as many criteria statements that they need. I use the following code and it adds the contol fine but the presenter is always coming back as null. If I add the view to the form it initializes the presenter fine but I need to do this dynamically. Any Ideas?

CriteriaView ctlPickQueryField = new CriteriaView();
ctlPickQueryField.Name = "ctlPickQueryField_" + tFieldNumber;
ctlPickQueryField.ClauseNumber = tFieldNumber;

Jul 8, 2006 at 12:06 PM
originally posted by: austinlamb

You need to use ObjectBuilder to build your CriteriaView - otherwise it doesn't have Dependency Injection done on it, so no presenter is injected.

So, you could do something like:

CriteriaView ctlPickQueryField = WorkItem.Items.AddNew<CriteriaView>();

Assuming that wherever this code is executing, it can access the WorkItem, that'd be a way to do it.
Jul 8, 2006 at 12:36 PM
originally posted by: TimLarsonEP

Thank you so much for your help. I ended up using this:

CriteriaView ctlPickQueryField = _presenter.WorkItem.Items.AddNew<CriteriaView>();

And it works like a champ. Sorry if I ask simple questions but I am just trying to get a grasp on this tool.

Thanks again,
Jul 8, 2006 at 1:16 PM
originally posted by: austinlamb

No problem :) Dependency Injection took a while for me to wrap my brain around as well - I'm so used to just new'ing up an object and using it, that being forced to ask my DI container (ObjectBuilder, in this case) to construct the object for me...it took some time to get used to.

Glad it worked for you!