-1

I'm trying to determine from my 2 dynamic buttons who click the event handler.

How i create my 2 dynamic buttons at Form load

private void Form1_Load(object sender, EventArgs e)
    {     
        for (int q = 0; q < 2; q++)
        {
            Point newLoc = new Point(a, b);              
            for (int i = 0; i <= 3 - 1; i++)
            {
                buttonArray[i] = new Button();
                buttonArray[i].Size = new Size(95, 80);
                buttonArray[i].Name = "btn" + q;
                buttonArray[i].Text = "btn" + q;
                buttonArray[i].Click += newButton;
                buttonArray[i].Location = newLoc;
                a = a + 10;
                if (a > 300)
                {
                    b = b + 100;
                    a = 1;
                }
                this.Controls.Add(buttonArray[i]);
            }
        }                 
    }

The Event I'm trying to call

   void newButton(object sender, EventArgs e)
    {
        if (sender == "btn1")
        {
            MessageBox.Show("btn1");          
        }

        if (sender == "btn2")
        {
            MessageBox.Show("btn2");
        }
    }

It can call the event handler if I do not add the IF statement.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

2 Answers2

2

You need to cast the object sender to a Buttonand then test against the Name property:

void newButton(object sender, EventArgs e)
{
    Button btn = sender as Button;

    // btn could be null if the event handler would be 
    // also triggered by other controls e.g. a label
    if(btn != null) 
    {
        if (btn.Name == "btn1")
        {
            MessageBox.Show("btn1");          
        }

        if (btn.Name == "btn2")
        {
            MessageBox.Show("btn2");
        }
    }
}

In C# 7.0 you can simplify that call:

void newButton(object sender, EventArgs e)
{
    if(sender is Button btn) 
    {
        if (btn.Name == "btn1")
        {
            MessageBox.Show("btn1");          
        }

        if (btn.Name == "btn2")
        {
            MessageBox.Show("btn2");
        }
    }
}
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
  • Another option is `if(btn == btn1)`. For `switch/case` using `string` is a good idea, though I'd use `switch(btn.Name) { case btn1.Name: ...` (or `nameof`), because string literals are immune to refactor. – Sinatr Jun 27 '17 at 09:56
  • 1
    @Sinatr The problem is, that there is no `btn1` reference – Romano Zumbé Jun 27 '17 at 10:48
0

You have 2 problems.
One :

buttonArray[i].Name = "btn" + q;
buttonArray[i].Text = "btn" + q;

You should create and pass new value for each button :

int temp = q;
buttonArray[i].Name = "btn" + temp;
buttonArray[i].Text = "btn" + temp;

More details

Two :

if (sender == "btn1")
{
    MessageBox.Show("btn1");          
}

You're comparing a Button object to a string. What you want to do is to check if your sender is Button at first and then check it's Name property.

Button btn = sender as Button;
if(btn != null)
{
    if (btn.Name == "btn1")
    {
        MessageBox.Show("btn1");
    }
}
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30