简体   繁体   中英

Pass data from Form1 to class1 using delegate and event

I know how to pass data using event and delegate from Form2 to Form1 (backwards actually). But I would like to know how to do it appropriately from Form1 (main form) to Form2. Imagine Form2 and some center form to show some progress (with a progressbar) which would be common for plenty of other forms.

This is how I would like to look like:

public partial class Form2 : Form
{
    public delegate void ProgressEvent(object obj);
    public event ProgressEvent OnProgressShowing;

    public Form2()
    {

    }

    private void ShowingProgress(object obj)
    { 
         //calling this method from Form1
         //But it must be PRIVATE!
    }
}

How to do it?

Like?

     Form2 f2 = new Form2();
     f2.OnProgressShowing += new Forms.ProgressEvent(?? ?what to put inside here?? I cannot access a private method on form2 ???);

I know one option is to create delegate and event on Form1, and pass a Form1`s reference to Form2, and subscribe to an event from Form2. But this is not what I would like. I would then have plenty of delegates and events in each of the other forms which would call this form2.

Since your first form is creating an instance of your second form and "owning" that instance, it is appropriate design for the method on Form2 that updates the UI based on the current progress to be public , and for the other forms calling it to call that method. There is also no need for Form2 to have an event at all, since it is not the one informing other forms that something has happened.

In this case, when creating a design document to indicate the relationships between these forms, we can say that Form1 , or other forms have a Form2 . A HAS-A relationship makes sense here. For Form2 it's goal is simply to display progress based on information provided by another class, so having a public method for another class to tell it to display progress is correct .

You're missing up a reversed relationship. If the child form needed to inform the parent forms of something, then Form2 would have an event, and in the handler of that event the forms creating it (ie Form1 ) would generally be calling their own private methods in the handler, or possibly accessing other public methods of Form2 to pull or push data out of it.

The idea here is that is'a appropriate for Form1 to "know about" Form2 . It's appropriate for it to have a reference to it, and to know about whatever it exposes publicly. Form2 , however, shouldn't know anything about what form creates it. It should be able to display progress for any form that can tell it what progress to show. If it needs to accept a reference to Form1 or know anything about it at all, then that can't happen. By using an event on Form2 when it needs to pass information out to the form that creates it, it can avoid needing to know what form that is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM