gpt4 book ai didi

C# 单击一个按钮上的多个事件?

转载 作者:行者123 更新时间:2023-12-02 18:21:16 24 4
gpt4 key购买 nike

我是 C# 新手,已经慢慢学习了几周,并且了解一些基本的 JavaScript。

我不想让三个按钮将不同按钮上的字符串或整数的值更改为不同的值,然后启动所有按钮共享的公共(public)事件。我知道我可以复制并粘贴它们,但我想让代码简短。

protected void btn1_Click(object sender, EventArgs e)
{
string example = a;
//Start the MainEvent
}
protected void btn2_Click(object sender, EventArgs e)
{
string example = b;
//Start the MainEvent
}
protected void btn3_Click(object sender, EventArgs e)
{
string example = c;
//Start the MainEvent
}
protected void MainEvent(object sender, EventArgs e)
{
//Content of MainEvent. Result of MainEvent is determined by the value of "Example".
}

最佳答案

您可以仅使用一个事件处理程序来完成此操作:

btn1.Click += MainEvent;
btn2.Click += MainEvent;
btn3.Click += MainEvent;

protected void MainEvent(object sender, EventArgs e)
{
string example;
if(sender == btn1)
{
example = a
}
else if(sender == btn2)
{
example = b
}
else if(sender == btn3)
{
example = c
}

//Do whatever with example
}

关于C# 单击一个按钮上的多个事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18802380/

24 4 0
文章推荐: javascript - 更改非事件页面标题
文章推荐: javascript - 错误: Object # has no method 'easeInOutCubic' - jquery-ui