How to have Controls.AddRange to add collectionbase

  • Thread starter smartscottiedog
  • 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.
 
A

Armin Zingler

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())
 
S

smartscottiedog

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top