Link to home
Start Free TrialLog in
Avatar of kranthi4uonly
kranthi4uonly

asked on

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
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

You can do this if the controls are textboxes. If you have other type you can add to this code

{
   
    foreach (Control ctrl in this.Controls) {
        if (ctrl is TextBox) {
            ctrl.Enabled = false;
        }
    }
}
You could place all controls into a single Panel control and use the Enabled property of the Panel control which will impact all controls in the Panel at once.

Good Luck
Avatar of p_davis
p_davis

to iterate through the controls jpaulino is correct you can do foreach(Control ctr in this.Controls) but if you have something like panels you will need to do a sub iteration like
 if(ctrl.gettype() == typeof(panel))
 foreach(control panelctrl in ctrl)
{}

you can use the get type to determine what kind of control it is and with that you can use 'as' to get at its properties

so if ctrl is a text box
if(ctrl.gettype == typeof(textbox)
{
    textbox tBox = ctrl as textbox;
    //then use tbox to set the properties as need (ie enabled, visible, readonly (if applicable)
}
obviously i made a few typ'os but you should get the idea -- if not, post back
You dont always need to iterate through the panels.  For example if you have a set of controls for entering a new entry into a form, you dont want those controls available to interact with until the "new" event is fired you want to open this panel up not all panels.

I guess there is two ideas here that will work it is more a case of what will fit the program better as it depends on your exact situation and what your trying to achieve.
Dux11 i would agree with you, but for the fact that the asker stated
**How to make all the controls readonly at runtime ** and if there are any controls on the panels you would need to iterate through them.

(i don't know what you mean by "the "new" event")
If the controls are on the panel and you disable the panel at runtime it also disables all the controls.  I use this method for times when I have an entry form but I dont want the user to be able to use the controls for adding a new item until they trigger it by for example clicking a command button to enter a new entry.

This saves having to loop through all controls and taking their type into account also.

It depends on the situation on where I would use either so was just adding an extra way to achieve this.  I can see times when either method would be better than the other.
ok, i didn't see your point from your first point, but do now. --and a good point it is
Avatar of kranthi4uonly

ASKER

thanks for ur reply iam using the follwing  but its not working so is this the right way
foreach (Control ctrl in this.Controls)
            {
                if (ctrl.GetType() == typeof(Panel))
                {
                    if (ctrl.GetType() == typeof(UserDefinedTextBox))
                    {
                        UserDefinedTextBox  tb = ctrl as UserDefinedTextBox;
                        tb.ReadOnly = true;
                    }
                }
            }
i have  5 panels in my form thanks
ihad disabled the panle as well

Addpanel.Enabled = false;
            Savebtn.Enabled = true;
            Addpanel.VerticalScroll.Enabled = true;
iam having a scroll bar in my panel even that is disable even i had set that to truw its not enabled thanks
ASKER CERTIFIED SOLUTION
Avatar of p_davis
p_davis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
iam using the following code
iam geting exception in the last line tb.readonly=true;
iam  getiing object reference  not set to an instance of an  object exception
i tried to use the new operator also
foreach (Control ctrl in this.Controls)
            {
                if (ctrl.GetType() == typeof(Panel))
                {
                    foreach (Control panelControl in ctrl.Controls)
                    {
                        if (panelControl.GetType() == typeof(GroupBox))
                        {
                         foreach (Control tbox in PanelControl.Controls)
                            {
 
                       if (tbox.GetType() == typeof(UserDefinedTextBox))
                                        {
                    UserDefinedTextBox tb = ctrl as UserDefinedTextBox ;
                                            tb.ReadOnly = true;
                                        }
                                    }
                                }
                            }
                        }
 
                    }

Open in new window

ohh  got it i made a mistake in the last line

UserDefinedTextBox tb = tbox  as UserDefinedTextBox ;
                                     
instead of using this i had used this one
 UserDefinedTextBox tb = ctrl as UserDefinedTextBox ;
                                     
sorry
and thank you very much