Make all Controls on a Form read-only at once

前端 未结 7 531
故里飘歌
故里飘歌 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:10

    In Form:

    if (_cached == null)
    {
        _cached = new List();
    
        foreach(var control in Controls)
        {
            TextBox textEdit = control as TextBox;
            if (textEdit != null)
            {
                textEdit.ReadOnly = false;
                _cached.Add(textEdit);
            }
        }
    } 
    else
    {
        foreach(var control in _cached)
        {            
            control .ReadOnly = false;
        }
    }
    

    Add recursion also (Controls can be placed into other controls (panels)).

提交回复
热议问题