Form.ActiveForm 属性

定义

获取此应用程序的当前活动窗体。

C#
public static System.Windows.Forms.Form ActiveForm { get; }
C#
public static System.Windows.Forms.Form? ActiveForm { get; }

属性值

Form

Form,表示当前活动窗体,或者如果没有活动窗体,则为 null

示例

以下示例获取活动窗体并禁用窗体上的所有控件。 该示例使用 Controls 窗体的集合循环访问窗体上的每个控件并禁用控件。

C#
public void DisableActiveFormControls()
 {
    // Create an instance of a form and assign it the currently active form.
    Form currentForm = Form.ActiveForm;
    
    // Loop through all the controls on the active form.
    for (int i = 0; i < currentForm.Controls.Count; i++)
    {
       // Disable each control in the active form's control collection.
       currentForm.Controls[i].Enabled = false;
    }
 }

注解

可以使用此方法获取对当前活动窗体的引用,以对窗体或其控件执行操作。

如果应用程序是多文档接口 (MDI) 应用程序,请使用 ActiveMdiChild 该属性获取当前活动的 MDI 子窗体。

适用于

产品 版本
.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
Windows Desktop 3.0, 3.1, 5, 6, 7

另请参阅