How to have Controls.AddRange to add collectionbase

  • Thread starter Thread starter smartscottiedog
  • Start date Start date
S

smartscottiedog

Hi,
I have a class where it has Button in collection and it is declared
like this.

public class ButtonArray : System.Collections.CollectionBase

It has methods to add and delete buttons and handles clicks, etc.
Works fine as is in a form.

I would like to add this to Controls.AddRange of FlowLayoutPanel which
expects Array of Control. How do I convert ButtonArray to Control
Array? Do I add ICollection and implement CopyTo? Or is there a
simpler solution?

Syntax example is greatly appreciated. Thanks.
 
smartscottiedog said:
Hi,
I have a class where it has Button in collection and it is declared
like this.

public class ButtonArray : System.Collections.CollectionBase

It has methods to add and delete buttons and handles clicks, etc.
Works fine as is in a form.

I would like to add this to Controls.AddRange of FlowLayoutPanel which
expects Array of Control. How do I convert ButtonArray to Control
Array? Do I add ICollection and implement CopyTo? Or is there a
simpler solution?

Syntax example is greatly appreciated. Thanks.


Sorry, only VB.Net, but you'll get the point:


Dim o As New ButtonArray

'...
Controls.AddRange(o.Cast(Of Control)().ToArray())
 
Sorry, only VB.Net, but you'll get the point:

  Dim o As New ButtonArray

  '...
  Controls.AddRange(o.Cast(Of Control)().ToArray())

That did it. Here is the c#.

Controls.AddRange(o.Cast<Control>().ToArray())

Thanks again.
 
Back
Top