Search code examples
c#winformseventssignaturecustom-button

How i can change signature of custom button c#


I have my custom button:

public partial class RaiseEvent : Button
{

 public RaiseEvent()
{
 InitializeComponent();
}

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

}

And i need to change the signature like this:

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

FULL EDIT :I have a DLL where are 2 class, one CatchEvent for customEvent:

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

And one class 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);
}

With suggest in commentary for my Form:

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
}

So my last problem is to recover value from my class CatchEvent without class legacy. Thanks you for help !


Solution

  • While you can't really change the signature, you can slip an expanded set of argument data into the event call if you use a custom Button class.

    Here you provide an custom set of arguments in your custom button class by overriding the Click maybe like so :

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

    Now you can access the data in your instance click events by casting the EventArgs e to your CatchEvent class:

    private void button1_Click(object sender, EventArgs e)
    {
        CatchEvent catchEvent = (CatchEvent)e;
        Console.log(catchEvent.EventEnergy);
        button1.Text = catchEvent.data; 
    }