Cascading delegates

A

Amien Crombie

Hi

I have a Usercontrol (child) that is part of another usercontrol
(Parent). In my child usercontrol, I use a delegate to pass an event
with some data (a custom EventArgs class) to the parent usercontrol.
This parent usercontrol then creates a duplicate delegate that passes
this to my windows form.
This all works but maybe there is a better way of doing this ??
My parent usercontrol is nothing else than a container (FlowLayoutPanel)
for my child usercontrols.
When my app starts, it reads a database and creates child usercontrols
and places it on the parent usercontrol. This includes creating the
event handler coming from each child usercontrol.
Any advise would be appreciated.
Thanks
Amien.
Cape Town
www.tlabs.ac.za
 
M

Morten Wennevik [C# MVP]

Hi Amien,

Nothing wrong with this approach. However, I suspect the actual data flow
isn't related to the actual user controls rather than the objects they
display. You might consider moving the event flow to the object level.

For instance, if the parent Control displays a parent object and the
UserControl displays a child object, the parent object may subscribe to
events the child objects may throw and the parent control will in turn
display the result on the parent control through databinding. Also, the part
that does the actual update between the object is a separate object called a
Controller or a Presenter.

If you want to read up on this, there is a Model-View-Presenter pattern as
well as a Model-View-Controller pattern. MVC is more or less the same as MVP
although somewhat stricter in separating the View from the Model.
 
A

Amien Crombie

Morten said:
Hi Amien,

Nothing wrong with this approach. However, I suspect the actual data flow
isn't related to the actual user controls rather than the objects they
display. You might consider moving the event flow to the object level.

For instance, if the parent Control displays a parent object and the
UserControl displays a child object, the parent object may subscribe to
events the child objects may throw and the parent control will in turn
display the result on the parent control through databinding. Also, the part
that does the actual update between the object is a separate object called a
Controller or a Presenter.

If you want to read up on this, there is a Model-View-Presenter pattern as
well as a Model-View-Controller pattern. MVC is more or less the same as MVP
although somewhat stricter in separating the View from the Model.

Hi Morten
Excellent advise. Thanks. We have moved to C# in some of our Process
Control system consisting of mainly embedded micro-controllers. Also
never used (or knew) about this MVC/MVP patterns.. Seems MVC would suit
our situation as the controller normally 'controls' our hardware. The
'Viewer' can typically be any number of graphical display types used by us.
Thanks
Amien
 
B

Ben Voigt [C++ MVP]

Amien Crombie said:
Hi

I have a Usercontrol (child) that is part of another usercontrol (Parent).
In my child usercontrol, I use a delegate to pass an event with some data
(a custom EventArgs class) to the parent usercontrol. This parent
usercontrol then creates a duplicate delegate that passes this to my
windows form.
This all works but maybe there is a better way of doing this ??

Using a manual event, you can directly subscribe handlers to the control
which fires the events, without the intermediate containers ever having to
forward them.
 
A

Amien Crombie

Ben said:
Using a manual event, you can directly subscribe handlers to the control
which fires the events, without the intermediate containers ever having
to forward them.
Hi Ben

Yes, that make sense now that I have played around with delegates/events
a bit.
The other concern was that I wrote the event handling code inside the
usercontrol. If I create the eventhandlers in my form, my form becomes
too messy with all the event handlers.
Therefore I'm using a delegate in the usercontrol to pass data via a
custom eventArgs class, to any form that wants to subscribe to this
delegate.
But I'm very new to C# and are grateful for any help. I'm also looking
at the MVC pattern suggested by Morten and it seems to fit our needs.

Thanks
Amien
 

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

Top