Search code examples
c#stringbuttonobject-address

C# address Button using a string


I have a Problem with addressing a button. I have many buttons in my program and I have a function which is used by every button. I'm getting the name of the last clicked button with this:

foreach (Control t in this.Controls)
{
    if (t.Focused)
    {
        ClickedButton = t.Name;
    }

}

Then I want to change the Text of the button:

ClickedButton.Text = "Whatever";

But I can't use ClickedButton as the name of the button.

Thank you in advance!


Solution

  • Assuming this is an event, you can just do something like this

    Button btn = (Button)sender;
    btn.Text = "Whatever";