我怎样才能以所描述的方式举办活动 [英] How can I raise an event in the described way
问题描述
嗨
我设计了一个带有一个按钮的C#表单应用程序,每按一次按钮,就会将1添加到int变量t。我希望程序在t = 5时给我一条消息。代码以下列方式编写。但我的问题是:我怎样才能完成这段代码,而无需在button1_Click函数中以事件委托方式或其他方式编写任何未来的代码?
namespace NumEv
{
公共部分类Form1:表格
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender,EventArgs e)
{
++ t;
}
....
}
}
什么我试过了:
namespace NumEv
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void EvHppn(对象发送者,EvArg e)
{
MessageBox.Show(sender.ToString()+ e.NUMget);
label1.Text = t.ToString();
}
private void button1_Click(object sender,EventArgs e)
{
NumTstrObj.NumEv + = EvHppn;
NumTstrObj.NumTest(++ t);
NumTstrObj.NumEv - = EvHppn;
}
NumTstr NumTstrObj = new NumTstr();
int t = 0;
}
公共类EvArg:EventArgs
{
private int _Num;
public EvArg(int num){this._Num = num; }
public int NUMget
{
get {return _Num; }
}
}
public delegate void NumEvHndlr(object sender,EvArg e);
class NumTstr
{
公共事件NumEvHndlr NumEv;
public void NumTest(int n)
{
if(n == 5&& this.NumEv!= null)
NumEv(this,new EvArg(N));
}
}
}
我想你需要用事件创建运算符(++)重载类..
试试这个: -
使用System;
命名空间WindowsFormsApplication1
{
class PlusPlusOperatorOver
{
int num;
公共活动NumEvHndlr NumEv;
public PlusPlusOperatorOver(int num)
{
this.num = num;
}
public void NumTest(int n)
{
if(n == 5&& this.NumEv!= null)
NumEv(this,新的EvArg(n));
}
public static PlusPlusOperatorOver operator ++(PlusPlusOperatorOver ppo)
{
ppo.num ++;
ppo.NumTest(ppo.num);
返回ppo;
}
public delegate void NumEvHndlr(object sender,EvArg e);
public void Reset(){
num = 0;
}
}
公共类EvArg:EventArgs
{
private int _Num;
public EvArg(int num){this._Num = num; }
public int NUMget
{
get {return _Num; }
}
}
}
在表单类中使用: -
<$ p $使用系统;
使用System.Windows.Forms;
命名空间WindowsFormsApplication1
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent() ;
plus.NumEv + = new PlusPlusOperatorOver.NumEvHndlr(this.EvHppn);
}
private void button1_Click(object sender,EventArgs e)
{
plus ++;
//plus.NumEv - = this.EvHppn;
}
PlusPlusOperatorOver plus = new PlusPlusOperatorOver(0);
int t = 0;
private void EvHppn(对象发送者,EvArg e)
{
MessageBox.Show(sender.ToString()+ e.NUMget);
label1.Text = t.ToString();
plus.Reset();
}
private void button2_Click(object sender,EventArgs e)
{
plus ++;
}
}
}
我希望这会对你有所帮助..
Hi
I have designed a C# form application with one button in which by each click on the button, 1 is added to int variable t. I want the program to give me a message when t=5. The codes are written in the following way. But my question: how can I complete this code without writing any future code in button1_Click function, in "event delegate" way or other ways?
namespace NumEv { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ++t; } .... } }
What I have tried:
namespace NumEv { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void EvHppn(object sender,EvArg e) { MessageBox.Show(sender.ToString() + e.NUMget); label1.Text = t.ToString(); } private void button1_Click(object sender, EventArgs e) { NumTstrObj.NumEv += EvHppn; NumTstrObj.NumTest(++t); NumTstrObj.NumEv -= EvHppn; } NumTstr NumTstrObj = new NumTstr(); int t = 0; } public class EvArg : EventArgs { private int _Num; public EvArg(int num) { this._Num = num; } public int NUMget { get { return _Num; } } } public delegate void NumEvHndlr(object sender, EvArg e); class NumTstr { public event NumEvHndlr NumEv; public void NumTest(int n) { if (n==5 && this.NumEv != null) NumEv(this, new EvArg(n)); } } }
I think you need to create Operator(++) overloading class with Events..
try this:-
using System; namespace WindowsFormsApplication1 { class PlusPlusOperatorOver { int num; public event NumEvHndlr NumEv; public PlusPlusOperatorOver(int num) { this.num = num; } public void NumTest(int n) { if (n == 5 && this.NumEv != null) NumEv(this, new EvArg(n)); } public static PlusPlusOperatorOver operator ++(PlusPlusOperatorOver ppo) { ppo.num++; ppo.NumTest(ppo.num); return ppo; } public delegate void NumEvHndlr(object sender, EvArg e); public void Reset() { num = 0; } } public class EvArg : EventArgs { private int _Num; public EvArg(int num) { this._Num = num; } public int NUMget { get { return _Num; } } } }
Use in the form class:-
using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); plus.NumEv += new PlusPlusOperatorOver.NumEvHndlr(this.EvHppn); } private void button1_Click(object sender, EventArgs e) { plus++; //plus.NumEv -= this.EvHppn; } PlusPlusOperatorOver plus = new PlusPlusOperatorOver(0); int t = 0; private void EvHppn(object sender, EvArg e) { MessageBox.Show(sender.ToString() + e.NUMget); label1.Text = t.ToString(); plus.Reset(); } private void button2_Click(object sender, EventArgs e) { plus++; } } }
I hope this will help you..
这篇关于我怎样才能以所描述的方式举办活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!