Button.PerformClick 方法

定义

生成按钮的 Click 事件。

C#
public void PerformClick();

实现

示例

下面的代码示例在另一个按钮的Button交替Click事件上生成 一个 Click 事件。 此代码假定已在窗体上实例化了两 Button 个控件,并且名为 myVar 的成员变量已在 类中声明为 32 位有符号整数。

C#
private void button1_Click (Object sender, 
                               EventArgs e)
 {
    // If myVar is an even number, click Button2.
    if(myVar %2 == 0)
    {
       button2.PerformClick();
       // Display the status of Button2's Click event.
       MessageBox.Show("button2 was clicked ");
    }
    else
    {
       // Display the status of Button2's Click event.
       MessageBox.Show("button2 was NOT clicked");
    }
    // Increment myVar.   
    myVar = myVar + 1;
 }

注解

可以调用此方法来引发 Click 事件。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅