Test whether a control is a container

G

Greg

Is there a simple way to test if a control is a container type control
(e.g. groupbox, panel etc)

I realise that all container controls have to by definition, have the
System.ComponentModel.DesignerAttribute. If there is an easy way to
query whether a control has this attribute, this may well be the way
forward. I'm not sure how to do this.

Or is there a different way?

Greg.
 
M

Matt

Greg said:
Is there a simple way to test if a control is a container type control
(e.g. groupbox, panel etc)

I realise that all container controls have to by definition, have the
System.ComponentModel.DesignerAttribute. If there is an easy way to
query whether a control has this attribute, this may well be the way
forward. I'm not sure how to do this.

Or is there a different way?

Greg.

I'm not quite sure what you are asking. Any control derived from the
Control
class will have a "Controls" member that can contain other controls. If
you
want to know if it can support children, the derivation from Control
will tell
you that.

Matt
 
M

Mythran

Greg said:
Is there a simple way to test if a control is a container type control
(e.g. groupbox, panel etc)

I realise that all container controls have to by definition, have the
System.ComponentModel.DesignerAttribute. If there is an easy way to
query whether a control has this attribute, this may well be the way
forward. I'm not sure how to do this.

Or is there a different way?

Greg.

Although Matt's reply is true, you could check the instance in question to
see if it implements the IContainer interface.

HTH,
Mythran
 
M

Matt

Mythran said:
Although Matt's reply is true, you could check the instance in question to
see if it implements the IContainer interface.

I knew I should have taken the extra second to look at the interfaces.
Yours
is a much better answer. Honestly, though, I'm still not sure what
problem he
is trying to solve. Virtually every control implements IContainer.

Matt
 
G

Greg

Thanks Matt and Mythran.

I currently have a method that does some work on a control reference
passed to it. It needs to be understand whether the control passed to
it is capable of containing other controls - the current list being for
a winforms app:
FlowLayoutPanel
Panel
SplitContainer
TabControl (and tabpage)
TableLayoutPanel
Groupbox
However, I need to be able to account for new types of container being
present, so merely listing the types of container type of control is
not sufficient.
If the control passed to the method is a container control, then the
method calls itself recursively using the reference of the controls
within it.

Thanks again,

Greg.
 
G

Greg

Perhaps the easiest method for what I need is to look at the number of
controls within the control - if that is more than 0 then I need to
call the same code for each control within that control, and so on.

Greg.
 
G

Greg

That didn't work at all - some of the controls that I have created are
composite controls. Now I'm back to the drawing board - how to test a
control to see if it is a container type control.
 

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