Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have several buttons to click, and all the same function (that I want to create), they just differ by controller's name. For example:

private void markX()
        {
            buttonName.Text = "X";
            buttonName.ForeColor = System.Drawing.Color.Red;
        }

How can I pass the button object that that is modified in the function into the function's parameters?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
469 views
Welcome To Ask or Share your Answers For Others

1 Answer

Make it a Click event handler, attach it to each button, and use the sender parameter as the button to change.

void button_Click(Object sender,  EventArgs e)
{
    var button = sender as Button;
    if(button != null)
    {
        button.Text = "X";
        button.ForeColor = System.Drawing.Color.Red;
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...