简体   繁体   English

在form1中从form2调用事件

[英]call event from form2 in form1

hi call event from form2 in form1? 您好在form1的form2调用事件吗?

for example : 例如 :

The following code into form2 : 将以下代码转换为form2:

    private void Form2_Load(object sender, EventArgs e)
    {
        MessageBox.Show("http://stackoverflow.com");
    }

What to write in a form1? 在form1中写什么?

Why are you wanting to call the event? 您为什么要致电此活动? Will you know the sender and the Event Args? 您知道发件人和事件Args吗?

Why don't you just create a public method in Form2 that Form1 is able to see? 您为什么不只在Form2中创建Form1能够看到的公共方法?

怎么样form2.Form2_Load(this, null)

You can't call private members of a class from outside it. 您不能从班级外部呼叫班级的私人成员。

You can change the accessibility to internal , which will make it visible within the assembly - if your form1 is in the same assembly. 您可以将internal的可访问性更改为internal ,这将使其在程序集中可见-如果您的form1在同一程序集中。

Alternatively you can make it a public method, which would make it globally accessible. 或者,您可以将其设置为public方法,从而使其可以全局访问。

However, you shouldn't call event handlers in such a manner - they are supposed to handle events that the declaring class raises. 但是,您不应以这种方式调用事件处理程序-它们应该处理声明类引发的事件。

For the sample code you gave, a better solution would be to create a public or internal method that can be called from this event handler: 对于您提供的示例代码,更好的解决方案是创建一个可从此事件处理程序调用的公共或内部方法:

private void Form2_Load(object sender, EventArgs e)
{
    MyMethod();
}

public MyMethod()
{
    MessageBox.Show("http://stackoverflow.com");
}

In order to call this method from form1 , it needs to know about form2 : 为了从form1调用此方法,它需要了解form2

 // in form1
 Form frm2 = new Form2();
 frm2.MyMethod();

You can't raise an Event from outside a class. 您不能从课堂外引发一个活动。 The convention is that you call a OnEventname method in the class. 约定是您在类中调用OnEventname方法。 Usually this method is protected (can't only accessed from the class itself or others that inherit from it) 通常,此方法是受保护的(不能仅从类本身或从其继承的其他类访问)

// in form1
private void Method1()
{
    using (var form2 = new Form2())
    {
        form2.Show();

        form2.RaiseLoadEvent(EventArgs.Empty);
    }
}

// Create this method in form2
public void RaiseLoadEvent(EventArgs e)
{
    OnLoad(this, e);
}


// The OnLoad method already exists in form 2 
// But it usually looks like this
protected void OnLoad(EventArgs e)
{
    var eh = LoadEventHandler;
    if (eh != null) 
    {
       eh(this, e); 
    }
}

But I don't suggest to raise the LoadEvent, because It is raised only once after the creation of the form. 但我不建议引发LoadEvent,因为它在创建表单后仅被引发一次。 More usual is to react to the Load event to modify the form. 更常见的是对Load事件作出反应以修改表单。

privat void Method1()
{
    using (var form2 = new Form2())
    {
        // Add Event Handler
        form2.Load += new EventHandler(form2_Load);
        form2.ShowDialog();
    }
    // Allways remove Event Handler to avoid memory leaks
    form2.Load -= new EventHandler(form2_Load);
} 

private void form2_Load(object sender, EventArgs e)
{
    form2.Text = "Hello from form1";
}

Form1 (the event publisher) should expose a separate, public event property for Form2 (the subscriber) to subscribe to. Form1(事件发布者)应公开一个单独的公共事件属性,供Form2(订阅者)订阅。

For example: the form publishing the event will look like this: 例如:发布事件的表单将如下所示:

public partial class Publisher : Form
{
    public event PostUpdateHandler OnPostUpdate;

    public Publisher()
    {
        InitializeComponent();

        new Subscriber(this).Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (OnPostUpdate != null)
        {
            OnPostUpdate(new PostUpdateArgs(textBox1.Text));
        }
    }
}

public delegate void PostUpdateHandler(PostUpdateArgs args);
public class PostUpdateArgs : EventArgs
{
    public string UpdateText;

    public PostUpdateArgs(string s)
    {
        UpdateText = s;
    }
}

The subscribing form looks like this: 订阅表单如下所示:

public partial class Subscriber : Form { public Subscriber() { InitializeComponent(); 公共部分类Subscriber:表单{public Subscriber(){InitializeComponent(); } }

    public Subscriber(Publisher publisher) : this()
    {
        publisher.OnPostUpdate += new PostUpdateHandler(publisher_OnPostUpdate);
    }

    private void publisher_OnPostUpdate(PostUpdateArgs args)
    {
        this.Form2_Load(null, null);
    }

    private void Subscriber_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Dispose();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        MessageBox.Show("http://stackoverflow.com");
    }
}

When the user presses button1 on the publishing form, the subscribing form will execute the code associated with the delegate, resulting in a message box popping up with the message http://stackoverflow.com . 当用户在发布表单上按下button1时,订阅表单将执行与委托相关联的代码,从而导致显示一个带有消息http://stackoverflow.com的消息框。

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

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