下面的代码示例获取活动窗体并禁用该窗体上的所有控件。该示例使用窗体的 Controls 集合循环访问窗体上的每个控件并禁用这些控件。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; } }
{
// 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;
}
}