如何在C#web表单上使用事件委托?
我有两个.cs文件。我正在尝试在此项目中使用事件委托。Event.cs:如何在C#web表单上使用事件委托?,c#,webforms,delegates,eventhandler,C#,Webforms,Delegates,Eventhandler,我有两个.cs文件。我正在尝试在此项目中使用事件委托。Event.cs: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Project3 { public delegate int Calculate(int obj1, int obj2); public class Event { public event
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project3
{
public delegate int Calculate(int obj1, int obj2);
public class Event
{
public event Calculate CalculateNow;
int a = 0;
int b = 0;
int r = 0;
public int Add()
{
r = a + b;
return r;
}
public int Sub()
{
r = a - b;
return r;
}
public int Mul()
{
r = a * b;
return r;
}
public int Div()
{
r = a / b;
return r;
}
}
}
及表格1.cs:
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Project3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
label1.Text = btn.Text;
int num1 = Int32.Parse(textBox1.Text);
int num2 = Int32.Parse(textBox2.Text);
Event t = new Event();
t.CalculateNow +=new Calculate(t_CalculateNow);
int finalr = t.Add();
if (finalr != null)
{
label1.Text = "" + finalr;
}
else
{
label1.Text = "Error!";
}
}
}
}
我收到一个错误,说“当前上下文中不存在名称't_CalculateNow',我不知道如何更正它。希望有人能回答我。非常感谢你!如果有人能解释事件委托的逻辑,那将对我有很大帮助。您的代码缺少正确的事件初始化和使用方法。事件不能直接从其他类调用,但是可以创建方法为公共订阅者调用它(在我们的例子中是InvokeCalc方法)。看起来像这样的东西应该适合你:
public class Calculator
{
public event Calculate CalculateNow;
public delegate int Calculate(int x, int y);
public string InvokeCalc(int x, int y)
{
if(CalculateNow != null) return CalculateNow(x, y).ToString();
return null;
}
}
然后在winform中,您可以使用它:
namespace Project3
{
public partial class Form1 : Form
{
private Calculator calc;
public Form1()
{
InitializeComponent();
calc = new Calculator();
label1.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
label1.Text = btn.Text;
int num1 = Int32.Parse(textBox1.Text);
int num2 = Int32.Parse(textBox2.Text);
// subscribes a handler to your event
calc.CalculateNow += (n1, n2) => { return n1 + n2; }
// method that invokes your event on Calculator class
int finalr = calc.InvokeCalc(num1, num2);
if (finalr != null)
{
label1.Text = "" + finalr;
}
else
{
label1.Text = "Error!";
}
}
}
}
附言。
虽然这是一个说明如何使用事件的示例,但您不太可能以这种方式使用事件。通常,事件应该用于通知订阅者(其他类)事件已经发生。事件只能从类内部触发,而这种封装某种程度上提高了应用程序的完整性,因为否则任何类或结构都可以触发此事件,并且很难找出哪个类实际调用了它以及在哪里 好吧,它不存在于那种环境中,它存在于
事件类中(顺便说一句,我会重命名这个),你想实现什么?谷歌可以解释事件委托的逻辑。亚马逊也是一个很有用的资源,特别是如果你喜欢印刷材料的话。@Sayse事件将进行计算并返回最终答案。label1将在web表单中显示答案。听起来你根本不需要在这里举办活动。如果有的话,静态方法可以完成这项工作。(如果你真的想用一个单独的方法来做这件事)@Sayse是的,但我正在尝试学习事件委托
。如果我想把它应用到这个项目中,我该如何改变它?希望你能给我举个例子。非常感谢。我正在测试您的代码,但有一个错误:名称“Calculate”在当前上下文中不存在。@Alison对不起,我把代理名称Calculate
与事件名称CalculateNow
混淆了。。。我已经纠正了这个小小的困惑