你好:)我其实想问问题 所以我的目标是“如果我点击按钮1,按钮2也会被点击”
“可以吗?点击一个按钮,然后点击另一个按钮?”
这是我的代码:
Button btn1 = sender as Button;
if (btn1 == button1){
button2.PerformClick();
}
它实际上不起作用似乎有什么不对
答案 0 :(得分:2)
我建议提取方法。
在:
private void button1_Click(object sender, EventArgs e)
{
Routine 1 code ...
Routine 2 code ... // <- do not copy yourself; copy + paste is evil!
}
private void button2_Click(object sender, EventArgs e)
{
Routine 2 code ...
}
后:
//TODO: think over the right name
private void Routine1()
{
Routine 1 code ...
}
//TODO: think over the right name
private void Routine2()
{
Routine 2 code ...
}
...
private void button1_Click(object sender, EventArgs e)
{
Routine1();
Routine2();
}
private void button2_Click(object sender, EventArgs e)
{
Routine2();
}
答案 1 :(得分:1)
这很简单。
private void button1_Click(object sender, EventArgs e)
{
if ((sender as Button) == button1)
{
button2_Click(sender, e);
}
}
private void button2_Click(object sender, EventArgs e)
{
}
答案 2 :(得分:1)
除非你有这样做的奇怪理由,否则不要!
你应该喜欢这样的东西:
void button1_Click(object sender, EventArgs e)
{
DoWork1();
DoWork2();
}
void button2_Click(object sender, EventArgs e)
{
DoWork2();
}