Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>it should be:</p> <pre><code>void btn_Click(object sender, EventArgs e) { Button btn = sender as Button; btn.Text = "button name changed"; // here problems occurs } </code></pre> <p>the sender is the button itself, not the array</p> <p>UPDATE ACCORDING TO THE COMMENT:</p> <p>you should do something like this:</p> <pre><code>public partial class Form1 : Form { private Button[] m_ButtonsArray; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { m_ButtonsArray = new Button[5]; for (int i = 0; i &lt; 5; i++) { m_ButtonsArray [i] = new Button(); m_ButtonsArray [i].Width = 50; m_ButtonsArray [i].Height = 50; flowLayoutPanel1.Controls.Add(m_ButtonsArray [i]); m_ButtonsArray [i].Click += new EventHandler(btn_Click); } } void btn_Click(object sender, EventArgs e) { m_ButtonsArray[3].Text = "button name changed"; // here problems occurs } } </code></pre> <p>UPDATE - Changing the 2nd button when the 5th button was clicked:</p> <pre><code>public partial class Form1 : Form { private Button[] m_ButtonsArray; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { m_ButtonsArray = new Button[5]; for (int i = 0; i &lt; 5; i++) { m_ButtonsArray [i] = new Button(); m_ButtonsArray [i].Width = 50; m_ButtonsArray [i].Height = 50 m_ButtonsArray [i].Tag = i;; flowLayoutPanel1.Controls.Add(m_ButtonsArray [i]); m_ButtonsArray [i].Click += new EventHandler(btn_Click); } } void btn_Click(object sender, EventArgs e) { Button btn = (Button)sender; if (((int)btn.Tag) == 5) { m_ButtonsArray[2].Text = "your text here"; } } } </code></pre> <p>explanation: when i create the button, i add the index as a tag (the button knows it's index with the <code>Tag</code> property) when a button is clicked, i check the sender object - casting it to <code>Button</code>, for the index that kept in the <code>Tag</code>. if the <code>Tag</code> value is 5 then i reference the second button (i can reference it though <code>m_ButtonsArray</code>) and change the values of it</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload