Home > database >  How to pass more params to Winform control event?
How to pass more params to Winform control event?

Time:12-24

I try to pass 3 extra int params to a button1_Click event

private void button1_Click(object sender, EventArgs e, int Xaxis, int Yaxis, int Zaxis)
    {
        switch (Xaxis)
        ...
        textBox1.Text = "Finished";
    }
...

To achieve ↓↓↓

...
public void Func1()
   {
      button1_Click(sender, e, 16, 5, 59)   //Pass 16 5 59 to event button1_Click
      //To perform a click event simulation with designated params
      ...
   }

private void button2_Click(object sender, EventArgs e)
    {
        ...
        Func1();
    }

But there is something wrong with Form1.Designer.cs

this.button1.Click  = new System.EventHandler(this.button1_Click);
//Compiler Error CS0123: No overload for 'method' matches delegate 'delegate'

How to pass 3 extra int params to a button1_Click event correctly?

CodePudding user response:

I suggest extracting a method, let's separate business logic ( axis routine) and UI (button click event handling):

private void MyClick(Button button, int Xaxis, int Yaxis, int Zaxis) {
  //TODO: All business logic here
  switch (Xaxis)
  ...
  textBox1.Text = "Finished";
}

Then use it

// Just UI event handler
private void button1_Click(object sender, EventArgs e) {
  //TODO: put right axis values here
  MyClick(sender as Button, 0, 0, 0);
}

public void Func1()
{
  MyClick(button1, 16, 5, 59);   // Pass 16 5 59 
  //To perform a click event simulation with designated params
  ...
}

// Another UI event handler
private void button2_Click(object sender, EventArgs e)
{
    ...
    Func1();
}

  • Related