我这里有一个多按钮事件处理程序,但如果点击了一个按钮,我只需要针对它,我该怎么做


private void btnCodeAkas_Click(object sender, EventArgs e)
{
tmrTimerAkas.Start();
tmrTimerTwoAkas.Start();

}

但我只需要瞄准一个按钮,如果它被点击了,我该怎么做?

您可以将发送方强制转换为Control对象,这样您就可以获得按钮的名称,然后编写条件逻辑。

private void button1_Click(object sender, EventArgs e)
{
var control = sender as Control;
if (control.Name == "button1")
{
Console.WriteLine($"Clicked {control.Name} button");
}
else if (((Control)sender).Name == "button2")
{
Console.WriteLine($"Clicked {control.Name} button");
}
}

相关内容

最新更新