C# Custom Event Handler
I am a person learning c#, and I have a program with a Parent form and a Child form. 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开发者_StackOverflow社区 DatasourceUpdated is not defined. 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. 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 += ...
you are trying to attach your handler to the class try this instead
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);
The signature is being specified by the fact that you're declaring the event as being handled by System.EventHandler
, which has that signature. 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...?
.NET events have both a "sender" object and an "EventArgs" object. 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
. See the capital S? Also, don't forget the args and to declare the DataSourceUpdated
as an 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;
精彩评论