PC Review


Reply
Thread Tools Rate Thread

Cascading delegates

 
 
Amien Crombie
Guest
Posts: n/a
 
      17th Apr 2009
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


 
Reply With Quote
 
 
 
 
Morten Wennevik [C# MVP]
Guest
Posts: n/a
 
      17th Apr 2009
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.

--
Happy Coding!
Morten Wennevik [C# MVP]


"Amien Crombie" wrote:

> 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
>
>
>

 
Reply With Quote
 
Amien Crombie
Guest
Posts: n/a
 
      17th Apr 2009
Morten Wennevik [C# MVP] wrote:
> 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
 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      20th Apr 2009


"Amien Crombie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

> 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
>
>

 
Reply With Quote
 
Amien Crombie
Guest
Posts: n/a
 
      21st Apr 2009
Ben Voigt [C++ MVP] wrote:
>
>
> "Amien Crombie" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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.
>
>> 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
>>
>>

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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cascading? Chi Microsoft Access Forms 1 4th Nov 2009 09:26 PM
Can we declare delegates in class and can we declare delegates in Interface Bhuwan Bhaskar Microsoft Dot NET Framework 4 4th Oct 2007 05:25 AM
Cascading Delete not Cascading =?Utf-8?B?SmFzb24gV2VpZXI=?= Microsoft Dot NET Framework 0 11th Apr 2007 06:28 PM
Advanced use of delegates to automatically disconnect delegates from a set of events kristian.freed@gmail.com Microsoft C# .NET 2 18th Nov 2006 09:56 PM
PinvokeDLL or delegates or ??? ... Calling C# delegates from C++ as CALLBACK Roland Rehmnert Microsoft Dot NET Compact Framework 1 20th Feb 2005 12:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:26 AM.