简体   繁体   English

C#自定义事件处理程序

[英]C# Custom Event Handler

I am a person learning c#, and I have a program with a Parent form and a Child form. 我是一个学习c#的人,并且我有一个包含“父母”形式和“孩子”形式的程序。 I want the child form to raise an event so that the Parent form can do something. 我希望子窗体引发一个事件,以便父窗体可以执行某些操作。 I copied some code, but I am not smart enough to see what is wrong. 我复制了一些代码,但是我不够聪明,看不出什么毛病。 I don't know how to correctly code the event in the child form. 我不知道如何在子表单中正确编码该事件。 The error is DatasourceUpdated is not defined. 错误是未定义DatasourceUpdated。 Can anyone help me out with a suggested fix? 谁能帮我解决建议的问题?

In the Child form I have 在儿童形式中,我有

public partial class Form2 : Form
{
   public EventHandler DataSourceUpdated;
   ...
   private void button2_Click(object sender, EventArgs e)  //Done button
   {
       if (this.DataSourceUpdated != null) //raise the event
       {
           this.DatasourceUpdated();
       }

       this.Close();
   }

In the parent form I have this: 在父表格中,我有这个:

private void myAddRecord()
{
    string myID = string.Empty;
    string myMessage = "Insert";

    Form2 myForm = new Form2(myID, myMessage);

    Form2.DatasourceUpdated += ChildUpdated;
    myForm.Show();

Right now, you're declaring an EventHandler, not an event. 现在,您声明的是EventHandler,而不是事件。 Change this to: 将其更改为:

public partial class Form2 : Form
{   
    public event EventHandler DataSourceUpdated;
       ...   
    private void button2_Click(object sender, EventArgs e)  //Done button   
    {
       if (this.DataSourceUpdated != null) //raise the event       
        {           
            this.DataSourceUpdated(this, EventArgs.Empty);       
        }       
        this.Close();   
    }

Also, when you go to subscribe to your event, you need to subscribe to the event on the instance, not on the class: 另外,当您订阅事件时,需要在实例上而不是在类上订阅事件:

Form2 myForm = new Form2(myID, myMessage);
myForm.DataSourceUpdated+= ChildUpdated;
myForm.Show();

This is because the event is declared at the instance level, not statically. 这是因为该事件是在实例级别而非静态声明的。

Form2.DatasourceUpdated += ...

您正在尝试将您的处理程序附加到该类上,请改用

myForm.DatasourceUpdated += ...

Your code looks right, as far as I can tell, as long as you have an actual handler; 据我所知,只要您有实际的处理程序,您的代码就看起来正确。 you have not included that in your code. 您尚未将其包含在您的代码中。 ChildUpdated needs to be a method that with the signature void (object sender, EventArgs e) , and you should also raise the event like that this.DataSourceUpdated(this, null); ChildUpdated必须是一个带有签名标记void (object sender, EventArgs e) ,并且还应该引发类似this.DataSourceUpdated(this, null);的事件this.DataSourceUpdated(this, null);

The signature is being specified by the fact that you're declaring the event as being handled by System.EventHandler , which has that signature. 通过将事件声明为由具有该签名的System.EventHandler处理的事实来指定签名。 You can create your own delegates as well, if you want it to receive some special parameters or no parameters at all. 如果希望它接收某些特殊参数或根本不接收任何参数,则也可以创建自己的委托。

Also, you have an inaccurate casing in your example, this.DatasourceUpdated -> this.DataSourceUpdated , but I'll assume that's just in your example...? 另外,您的示例中有一个不正确的大小写, this.DatasourceUpdated > this.DataSourceUpdated ,但是我假设这只是您的示例...?

.NET events have both a "sender" object and an "EventArgs" object. .NET事件同时具有“发送者”对象和“ EventArgs”对象。 These need to be included when your event is called. 当您的事件被调用时,这些必须包括在内。

for example: 例如:

private void button2_Click(object sender, EventArgs e)  //Done button
{
    if (this.DataSourceUpdated != null) //raise the event
    {
        this.DatasourceUpdated(this, EventArgs.Empty);
    }

    this.Close();
}

First of all there's a small typo: DatasourceUpdated vs DataSourceUpdated . 首先有一个小错字: DatasourceUpdated vs DataSourceUpdated See the capital S? 看到大写字母S吗? Also, don't forget the args and to declare the DataSourceUpdated as an event : 另外,不要忘记args并将DataSourceUpdated声明为event

public event EventHandler DataSourceUpdated;

...

this.DataSourceUpdated(this, EventArgs.Empty); 

Another problem I notice is that your calling a static member when you should be calling an instance member: 我注意到的另一个问题是,您应该在调用实例成员时调用静态成员:

Form2.DatasourceUpdated += ChildUpdated;

to

myForm.DatasourceUpdated += ChildUpdated;

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

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