Jocelyn
Jocelyn

Reputation: 153

C# Collection for controls readonly

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

Answers (1)

Tim Schmelter
Tim Schmelter

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

Related Questions