I have 2 forms. Form1 and Form2
Form 2 has the following code in it.
/// Delegate used for Reset
public delegate void ResetEventHandler(object sender, ResetPathProfileEventArgs e);
public event ResetEventHandler ResetPathProfileEvent;
protected virtual void RaiseResetEvent(string status)
{
// Raise event if something is listening
if (ResetPathProfileEvent != null)
{
var args = new ResetPathProfileEventArgs { Status = status };
ResetPathProfileEvent(this, args);
}
}
Now in form1, i wrote the following code
var frm = new Form2();
frm.ResetPathProfileEvent += frm_ResetPathProfileEvent;
frm.ShowDialog();
void frm_ResetPathProfileEvent(object sender, ResetPathProfileEventArgs e)
{
MessageBox.Show(e.Status);
}
There by I could successfully raise an event in form1 code based on user actions on form2.
Similarly i would like to let add the required code in form2 such that form1 can notify form2 back upon completion of expected user interaction on form1.
I am trying to explore various ways of doing that.
a) I can expose a public method on form2 and allow form1 to call it. b) define a delegate and an event on form1 referencing to a public method in form2
I am interested in defining delegate and event on form2 and achieve this callback functionality from from1.
Any hints???? +++++++++++++++++++++++++++++++++
let me now write how I achieved it
i have defined a new delegate in the same namespace which contains both form1 and form2
/// Delegate used for Redraw
public delegate void RedrawEventHandler(RedrawPathProfileEventArgs e);
I have declared a reference variable to this delegate
public RedrawEventHandler RedrawEvent;
registred for this event in form1 pointing to form2 call back function
var frm = new Form2();
frm.ResetPathProfileEvent += frm_ResetPathProfileEvent;
this.RedrawEvent += new RedrawEventHandler(frm.RedrawCallBackFn);
frm.Show();
invoking the event from form1 to form2 as needed
var args = new RedrawPathProfileEventArgs();
args.FileName = "xyz";
RedrawEvent(args);
I know that this is one of many options available to do this. But I need to do this as form1 is a third party vendor application and I am developing a plugin to it
I wanted to ensure a tight coupling between these 2 and also give very minimal work to form1 developer to integrate my form (form2)