c# - Pass data from Form1 to class1 using delegate and event -
i know how pass data using event , delegate form2 form1 (backwards actually). know how appropriately form1 (main form) form2. imagine form2 , center form show progress (with progressbar) common plenty of other forms.
this how like:
public partial class form2 : form { public delegate void progressevent(object obj); public event progressevent onprogressshowing; public form2() { } private void showingprogress(object obj) { //calling method form1 //but must private! } }
how it?
like?
form2 f2 = new form2(); f2.onprogressshowing += new forms.progressevent(?? ?what put inside here?? cannot access private method on form2 ???);
i know 1 option create delegate , event on form1, , pass form1`s reference form2, , subscribe event form2. not like. have plenty of delegates , events in each of other forms call form2.
since first form creating instance of second form , "owning" instance, appropriate design method on form2
updates ui based on current progress public
, , other forms calling call method. there no need form2
have event
@ all, since not 1 informing other forms has happened.
in case, when creating design document indicate relationships between these forms, can form1
, or other forms have form2
. has-a relationship makes sense here. form2
it's goal display progress based on information provided class, having public method class tell display progress correct.
you're missing reversed relationship. if child form needed inform parent forms of something, form2
have event, , in handler of event forms creating (i.e. form1
) calling their own private methods in handler, or possibly accessing other public methods of form2
pull or push data out of it.
the idea here is'a appropriate form1
"know about" form2
. it's appropriate have reference it, , know whatever exposes publicly. form2
, however, shouldn't know anything form creates it. should able display progress any form can tell progress show. if needs accept reference form1
or know @ all, can't happen. using event on form2
when needs pass information out form creates it, can avoid needing know form is.
Comments
Post a Comment