c# - Delegate with generic list signature for passing data to another form -
i'm quite new in c#, i'm struggling more 2 days. hope 1 can me out one.
below simplified code application.
i want pass list form1 form2 using delegate , event.
how can this? read tons of explanations events , delegates, still can't figure out, how works.
form1:
public delegate list<string> profileimporteventhandler(); public event profileimporteventhandler profileimported; private void btnimport_click(object sender, eventargs e) { // raise event onprofileimported(); } protected virtual void onprofileimported() { if (profileimported != null) // check if there subscribers { profileimported(); } }
form2:
public partial class form2 : form { form1 frm1; public form1() { // constructor logic frm1.profilechanged += new form1.profileimporteventhandler(form1_onprofileimported); } } list<string> form1_onprofileimported() { // }
update
none of solutions worked far. here have tried:
form 2
// use generic list profiles imported usb-stick private list<string> profilepaths = new list<string>(); public delegate void profileimporteventhandler(object sender, profileimporteventargs e); public event profileimporteventhandler profileimported; public delegate void importbuttonclickedeventhandler(); public event importbuttonclickedeventhandler buttonimportclicked; public delegate void havedatadelegate(ilist<string> data); public event havedatadelegate havedata; //.... private void btnimport_click(object sender, eventargs e) { // something... // raise event var ea = new profileimporteventargs(profilepaths); onprofileimported(ea); onbuttonimportclicked(); // when there data: var copy = havedata; // use copy avoid race conditions if (copy != null) { copy(profilepaths); } // close form this.dispose(); } protected virtual void onprofileimported(profileimporteventargs ea) { if (profileimported != null) // check if there subscribers { profileimported(this, ea); } } protected virtual void onbuttonimportclicked() { if (buttonimportclicked != null) { // fire event buttonimportclicked(); } }
form 1
public partial class frm_1 : form { // child form frm_2 frm2; public frm_1() { initializecomponent(); // something... // not sure if correct code , correct place frm2 = new frm_2(); frm2.profileimported += new frm_2.profileimporteventhandler(frm2_onprofileimported); //frm2.profileimported += frm2_onprofileimported; frm2.buttonimportclicked += new frm_2.importbuttonclickedeventhandler(frm2_buttonimportclicked); // in creation/init: frm2.havedata += datafromform2; } void frm2_onprofileimported(object sender, profileimporteventargs e) { // } void frm2_buttonimportclicked() { // } private void datafromform2(ilist<string> data) { // process data form2. } }
what still missing? thank help.
frm1.profilechanged += new form1.profileimporteventhandler(form1_onprofileimported);
[…]
list<string> frmloadprofileusb_onprofileimported()
first names not match. second, matching signatures not need (since c#2 if recall correctly) explicitly create delegate. thus:
frm1.profilechanged += frmloadprofileusb_onprofileimported;
however, think have event in wrong place. appears form2 trying pass data form1. event needs on form2, delegate passed data. thus:
in form2
public delegate void havedatadelegate(ilist<string> data); public event havedatadelegate havedata; // when there data: var copy = havedata; // use copy avoid race conditions if (copy != null) { copy(data); }
in form1
// in creation/init: form2instance.havedata += datafromform2; private void datafromform2(ilist<string> data) { // process data form2. }
Comments
Post a Comment