c# - Only fire the event on the correct User Control -


i have following scenario, have tabcontrol each tabpage represents year. on every year-tabpage have tabcontrol 12 tabpages months. on every month-tabpage have usercontrol. this:

foreach (tabpage tp2 in tbmonat.tabpages)             {                 ucmonat monat = new ucmonat(convert.toint32(tp.name), convert.toint32(tp2.name));                 monat.parent = this;                 onseturlaub += monat.getselectedcells;                 monat.dock = dockstyle.fill;                 tp2.controls.add(monat);             } 

on mainform have button, if button pressed event fired on usercontrol. @ moment event fired on every usercontrol. want fired on active usercontrol. how can that?

mainform:

public delegate void seturlaub(string date, eventargs e);     public event seturlaub onseturlaub;  public void cmdurlaub_click(object sender, eventargs e)     {                     if (onseturlaub != null)             onseturlaub(jahr + monat , e);     } 

usercontrol:

public void getselectedcells(string sender, eventargs e)     {         int year = convert.toint32(sender.substring(0, 4));         int month = convert.toint32(sender.substring(4));          int numcells = this.dgvurlaub.selectedcells.count;     } 

event handlers aren't selective in way. every subscriber receive event when raised , of usercontrols subscribe same event.

you can either filter when receive event

if (this.focused) {    //do stuff active control should } 

or can unsubscribe , subscribe focus changes, subscribing user controls enter , leave events

monat.enter += (s, e) => onseturlaub += ((ucmonat)s).getselectedcells; monat.leave += (s, e) => onseturlaub -= ((ucmonat)s).getselectedcells; 

so active ucmonat gets event.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -