我可以使用.NET控件父类启用/禁用它? [英] Can I use a .net control parent class to enable/disable it?

查看:142
本文介绍了我可以使用.NET控件父类启用/禁用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个委托一个多线程的程序,将启用/禁用各种控件。这似乎是合乎逻辑,使用一个处理所有的控制将是最好的选择,但我甚至不知道这是可能的。NET,如果是如何实现。

解决方案

 公共无效SetControlsEnabled(BOOL启用)
{
    //确保我们在正确的线程
    如果(InvokeRequired)
    {
        //如果没有,运行在UI线程的方法
        调用(新MethodInvoker(()=> SetControlsEnabled(启用)));
        返回;
    }

    //将所有控制code在这里,例如:
    // control1.Enabled =启用;
    // control2.Enabled =启用;

    //或者,做一个foreach(在控制控制C){...}

}
 

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.

解决方案

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) { ... }

}

这篇关于我可以使用.NET控件父类启用/禁用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆