Can I use a .net control parent class to enable/disable it?

南笙酒味 提交于 2019-12-13 02:57:39

问题


I need to write a delegate for a multi-threaded program that will enable/disable a variety of controls. It seems logical that using one handler for all controls would be the best choice, but I'm not even sure this is possible in .net and if so how to implement.


回答1:


public void SetControlsEnabled(bool enabled)
{
    // Make sure we're in the correct thread
    if (InvokeRequired)
    {
        // If not, run the method on the UI thread
        Invoke(new MethodInvoker(() => SetControlsEnabled(enabled)));
        return;
    }

    // Put all control code here, e.g:
    // control1.Enabled = enabled;
    // control2.Enabled = enabled;

    // Alternatively, do a foreach(Control c in Controls) { ... }

}


来源:https://stackoverflow.com/questions/1585163/can-i-use-a-net-control-parent-class-to-enable-disable-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!