Search code examples
c#winformsevents

Get the text value of the button that was clicked


I am trying to get the text value from a button that was clicked. In my head, it looks something like this:

private void button2_Click(object sender, EventArgs e)
{
    string s =  thisbutton.text
}

Solution

  • The object which fired the event is sender, so:

    private void button2_Click(object sender, EventArgs e)
    {
        string s = (sender as Button).Text;
    }