1

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 };
Jocelyn
  • 133
  • 1
  • 1
  • 10

1 Answers1

1

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
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939