我在我的应用程序中使用了 70 个按钮,所以我想与其制作不同的 button1_Click、button2_Click... button70_Click 事件,不如制作一个。问题是我不知道如何检查当前正在单击的按钮是什么,因为每当我单击一个按钮时我都希望它的颜色发生变化。
最佳答案
将事件处理程序的 sender
参数转换为 Button
Button ClickedButton = (Button)sender;
这是引发事件的按钮。
您可以在分配给每个按钮的点击事件的处理程序中使用它,例如
public form1()
{
//You probably do this in a loop over all your buttons
button1.Click += MyButtonClickHandler;
}
private void MyButtonClickHandler(object sender, EventArgs e)
{
Button ClickedButton = (Button)sender;
ClickedButton.ForeColor = Color.Red;
}
关于c# - 如何检查正在单击哪个按钮? Visual Studio C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36530156/