Make all Controls on a Form read-only at once

前端 未结 7 486
故里飘歌
故里飘歌 2021-01-12 07:40

Does anyone have a piece of code to make all Controls (or even all TextBoxes) in a Form which is read-only at once without having to set every Control to read-only individua

7条回答
  •  迷失自我
    2021-01-12 08:07

    I would use reflection to check to see if the generic Control object has an Enabled property.

    private static void DisableControl(Control control)
    {
        PropertyInfo enProp = control.GetType().GetProperty("Enabled");
        if (enProp != null)
        {
            enProp.SetValue(control, false, null);
        }
    
        foreach (Control ctrl in control.Controls)
        {
            DisableControl(ctrl);
        }
    }
    

提交回复
热议问题