vb.net listen for parent form event on each child form -


i trying implement function on parent form, when event fires, want perform actions on of child forms open. because given child form may or may not open @ given time, can't handle directly event on parent form: i.e., cant following child1 may not initiated @ time:

--parent form-- public sub parentevent()     doparentaction()     dochild1action()     dochild2action() end sub 

is there way on each child page listen parentevent() fired? essentially, want handle parentevent() being fired, on child page same if button clicked on child page, this:

--child1-- public sub childevent() handles parentform.doparentaction()     dochild1action() end sub 

this easy do, have step around vb's withevents , handles syntax @ it.

public class parentform     event ondosomething()      private sub dosomething()          raiseevent ondosomething()     end sub end class 

and then

public class childform    public sub new()         initializecomponent()         addhandler parentform.ondosomething, addressof dosomething     end sub      private sub dosomething()         '     end sub      private sub childform_formclosing(byval sender system.object, _                 byval e system.windows.forms.formclosingeventargs) _                 handles mybase.formclosing         removehandler parentform.ondosomething, addressof dosomething     end sub end class 

it's important make sure event handler removed before disposing child form (else end memory leak).

the above assumes using vb default instance of parentform - if you're not, have reference things accordingly. better approach might make parent argument in constructor like:

 public sub new(byval parent parentform)     initializecomponent()     addhandler parent.ondosomething, addressof dosomething  end sub   

also, of course, modifying removehandler section (you'd need keep reference parent). option hook/unhook in parentchanged event if mdi application.

the other caveat can't create of child forms in constructor of parent form since end self-reference during construction.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -