Question : How to make all the controls readonly at runtime in c# windows form

How to make all the controls readonly at runtime in c# windows form
i have some 20 controls on a form if user clicks button i want to show all the controls as readonly instead of making each control readonly is their a way to make all the controls as readonly at once  
thanks

Answer : How to make all the controls readonly at runtime in c# windows form

foreach (Control ctrl in this.Controls)
            {
                if (ctrl.GetType() == typeof(Panel))
                {
                    if (ctrl.GetType() == typeof(UserDefinedTextBox))
                    {
                        UserDefinedTextBox  tb = ctrl as UserDefinedTextBox;
                        tb.ReadOnly = true;
                    }
                }
            }
if you want to get at the controls on the panel you have to iterate through them
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
foreach (Control ctrl in this.Controls)
            {
                if (ctrl.GetType() == typeof(Panel))
                {
                    foreach(Control panelControl in ctrl.Controls)
                    {
                    if (ctrl.GetType() == typeof(UserDefinedTextBox))
                    {
                        UserDefinedTextBox  tb = ctrl as UserDefinedTextBox;
                        tb.ReadOnly = true;
                    }
                    }
                }
            }
Random Solutions  
 
programming4us programming4us