0

我有我的自定义按钮:

public partial class RaiseEvent : Button
{

 public RaiseEvent()
{
 InitializeComponent();
}

 private void button1_Click(object sender, EventArgs e)
 {}

}

我需要像这样更改签名:

private void button1_Click(object sender, CatchEvent e)
{}

完整编辑:我有一个 DLL,其中有 2 个类,一个用于 customEvent 的 CatchEvent:

public class CatchEvent : EventArgs
{
 public double data = 0;
 public CatchEvent(double value)
 {
 data = value;
 }
 public EventEnergy
 { 
 get { return this.data}
 }
}

还有一类Calcul:

public class Calcul
{
 public delegate void EventHandler(object sender, CatchEvent e);
 public event EventHandler<CatchEvent> NewData;
 public void Event(double value, byte[] data)
{
 //some calculs = double result
 OnRaiseCustomEvent(new CatchEvent(result));
}
 public virtual void OnRaiseCustomEvent (CatchEvent e)
{
 raiseEvent(this.e);
}

在我的表格的评论中提出建议:

public button1_Click (object sender, EventArgs e)
{
 double data = //i try to recover value from my class CatchEvent without legacy
 CatchEvent ce = new CatchEvent(data)
 textBox1.Text = ce.data.Tostring();
}
public Form1(Calcul pub)
{
 pub.NewData += button1; //doesnt work
}

所以我的最后一个问题是从我的类 CatchEvent 中恢复价值,而没有类遗产。谢谢你的帮助!

4

1 回答 1

3

虽然您无法真正更改签名,但如果您使用自定义 Button 类,则可以将一组扩展的参数数据滑入事件调用中。

在这里,您通过覆盖Click可能像这样在自定义按钮类中提供一组自定义参数:

protected override void OnClick(EventArgs e) 
{
    double data = // get data from DLL
    catchEvent ce = new catchEvent (data );
    base.OnClick(ce); 
}

EventArgs e现在,您可以通过将 转换为您的类来访问实例单击事件中的数据CatchEvent

private void button1_Click(object sender, EventArgs e)
{
    CatchEvent catchEvent = (CatchEvent)e;
    Console.log(catchEvent.EventEnergy);
    button1.Text = catchEvent.data; 
}
于 2020-06-15T07:36:35.843 回答