C# Collection for controls readonly

56 Views Asked by At

found the following code: Alle Controls einer Form auf Readonly setzen

But how have my Control.ControlCollection look like that I can use this method? I tried the following but it is not working:

Control[] ControlCollection = new Control[] { textbox1 };
1

There are 1 best solutions below

0
On

Change the parameter type to IEnumerable<Control> controls

You can also provide an extension method to convert ControlCollection to IEnumerable<Control>:

public static IEnumerable<Control> AsEnumerable(this ControlCollection controls)
    => controls?.Cast<Control>() ?? Enumerable.Empty<Control>();

So if you want to use your SetReadOnly method with ControlCollection:

SetReadOnly(this.Controls.AsEnumerable()); // all controls of the form are set readonly