简体   繁体   English

使用委托和事件将数据从Form1传递到class1

[英]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). 我知道如何使用事件将数据传递并从Form2委托给Form1(实际上是向后)。 But I would like to know how to do it appropriately from Form1 (main form) to Form2. 但是我想知道如何从Form1(主窗体)到Form2适当地做到这一点。 Imagine Form2 and some center form to show some progress (with a progressbar) which would be common for plenty of other forms. 想象一下Form2和一些中心表单显示一些进度(带有进度条),这对于许多其他表单是很常见的。

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. 我知道一个选择是在Form1上创建委托和事件,并将Form1的引用传递给Form2,并从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. 然后,我将以其他各种形式(称为“ 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. 由于您的第一种形式创建第二种形式的实例,并“拥有”这个实例,它是在方法适当的设计Form2 ,更新基于当前进度的UI是public ,而对于其他形式也打电话给呼叫该方法。 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. Form2根本不需要event ,因为它不是通知其他表单发生了事情的事件。

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 . 在这种情况下,当创建设计文档以指示这些表单之间的关系时,可以说Form1或其他表单具有 Form2 A HAS-A relationship makes sense here. HAS-A关系在这里很有意义。 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 . 对于Form2它的目标只是基于另一个类提供的信息显示进度,因此为另一个类提供一个公共方法来告诉它显示进度是正确的

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. 如果子窗体需要通知父窗体某些内容,则Form2将具有一个事件,并且在该事件的处理程序中,创建该窗体的窗体(即Form1 )通常将在处理程序中调用其自己的私有方法 ,或者可能访问Form2其他公共方法可以从中拉出数据。

The idea here is that is'a appropriate for Form1 to "know about" Form2 . 这里的想法是,适合Form1 “了解” 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. Form2 ,但是,不应该知道什么形式创造任何东西 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. 如果它需要接受对Form1的引用或完全不了解它,那么就不会发生。 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. 通过使用一个事件Form2 时,它需要将信息传递出去创建它的形式,这样可避免需要知道什么样的形式是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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