C#事件,如何引发它们? [英] C# events, how to raise them?
本文介绍了C#事件,如何引发它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用C#学习引发和处理事件.
I'm trying to learn raising and handling events in C#.
这是我的简单示例:
/////////////// creating a new BALL object ///
ballClass ball = new ballClass();
void button1_Click(object sender, EventArgs e)
{
// this should make the BALL object raise an event
ball.onHit();
label1.Text = "EVENT SEND";
}
// when event is fired, label text should change
void BallInPlayEvent(object sender, EventArgs e)
{
label2.Text = "EVENT FOUND!";
}
和球类:
class ballClass
{
public event EventHandler BallInPlay;
public void onHit()
{
this.BallInPlay ///// ??? how should i raise this event?
}
}
在电话中,我真的听不懂,我该如何发起活动?有什么想法吗?...:)
In the call I can't really understand, how do I raise the event? Any ideas?... :)
谢谢!
推荐答案
public void onHit()
{
if(BallInPlay != null)
BallInPlay(this, new EventArgs());
}
这篇关于C#事件,如何引发它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文