Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass more params to Winform control event?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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();
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading