0

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)

4

2 回答 2

0

Delegates (and events) are specifically meant to allow multiple subscribers to listen to an event. You could e.g. have four forms that listen to the event, and all of them are notified. In your case, you have just one form that is listening, but the choice to use an event is still the correct one.

However, the other way around, this doesn't sound logical and a public method would probably be a better choice. You can look at it this way. Form2 doesn't care who it sends the event to. There is just some event that has occurred and anyone that cares can listen to it (or not). The other way around, Form1 very much cares who receives the event. It know that Form2 needs to receive the event, and wants to send it specifically to Form2. So, a public method would be the way to go.

于 2013-06-25T05:00:38.820 回答
0

First of, I would seriously reconsider your design. Form1 owns Form2 but they are both going to respond to each others events; you have two way coupling and you are putting up Form2 as a dialog. Hmm..anyways:

Your different communication options are:

1) events, form1 exposes events form2 exposes events and they both subscribe to each others events.

2) event arguments. If form 1 wants to give feedback in the "frm_ResetPathProfileEvent" to form2, form2 can add stuff to ResetPathProfileEventArgs which can be manipulated by form1 and checked after the event has been raised.

3) methods, since form1 owns form2 it can call public methods to trigger behavior

4) Event aggregator pattern (hub and spokes model), a decoupling of subscribers and publishers (example impl: http://compositewpf.codeplex.com/SourceControl/latest#V4/PrismLibrary/Desktop/Prism/Events/EventAggregator.cs)

于 2013-06-25T05:57:04.467 回答