Reputation: 153
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 };
Upvotes: 1
Views: 59
Reputation: 460238
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
Upvotes: 1