禁用控件的速度太慢

本文关键字:速度 控件 | 更新日期: 2023-09-27 18:28:21

使用此方法:

    private void disableControls()
    {
        foreach (Control c in this.Controls)
        {
            c.Enabled = false;
        }
    }

在一个只有5个控件的窗体上,为什么它如此缓慢?

您可以清楚地看到每个控件都被禁用。

编辑:

以下是更多详细信息:

表单中唯一的事件处理程序是绑定到comboBox的IndexChanged。

我运行此方法的窗体是一个使用showDialog从父窗体调用的新窗体。

事实上,disable-controls方法是单击按钮时调用的第一个方法。

我真的不知道它为什么这么做,我会试着重新启动,看看它是否会好转。

禁用控件的速度太慢

无法复制。这里有一个简短但完整的程序,可以很快禁用控件。如果你能想出一个类似的慢的,我们可以找出为什么慢。

using System;
using System.Drawing;
using System.Windows.Forms;
class Test
{
    static void Main()
    {
        Form form = new Form();
        for (int i = 0; i < 4; i++)
        {
            Button button = new Button
            {
                Text = "Dummy",
                Location = new Point(10, i * 25)
            };
            form.Controls.Add(button);
        }
        Button disabler = new Button
        {
            Text = "Disable",
            Location = new Point(10, 100)
        };
        disabler.Click += delegate
        {
            foreach (Control c in form.Controls)
            {
                c.Enabled = false;
            }
        };
        form.Controls.Add(disabler);
        Application.Run(form);
    }                   
}