设定容器内控件是否可用

using System.ComponentModel;

        /// <summary>
        /// 例外
        /// </summary>
        public const string CS_EXCLUDE = "EXCLUDE";

代码
#region SetContainerControlEnabled
/// <summary>
/// 设定容器内控件是否可用
/// </summary>
/// <param name="parentCtrl">容器控件</param>
/// <param name="enabled">是否可用</param>
public static void SetContainerControlEnabled(Control parentCtrl, bool enabled)
{
foreach (Control ctrl in parentCtrl.Controls)
{
if (ctrl.GetType().FullName == "System.Windows.Forms.Label") continue;
if (ctrl.Tag != null && ctrl.Tag.ToString() == CS_EXCLUDE) continue;
PropertyDescriptorCollection pdc
= TypeDescriptor.GetProperties(ctrl);
PropertyDescriptor pdReadOnly
= pdc.Find("ReadOnly", false);
PropertyDescriptor pdEnabled
= pdc.Find("Enabled", false);
if (pdReadOnly != null)
{
pdReadOnly.SetValue(ctrl,
!enabled);
if (pdEnabled != null)
{
pdEnabled.SetValue(ctrl,
true);
}
}
else
{
if (pdEnabled != null)
{
pdEnabled.SetValue(ctrl, enabled);
}
}
}
}
#endregion
posted @ 2010-10-09 09:19  xfyn  阅读(122)  评论(0编辑  收藏  举报