How to determine if control is container...

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

This is probably pretty easy, but how does one determine if
a control is a CONTAINER control? I thought something like

If Typeof myControl is ContainerControl then ....

would work but that seems to always return FALSE.

Thanks.

Tom

--
 
That should work if the control is in fact an instance of a class that
inherits from ContainerControl somewhere down the line.
 
Hi Tom,

It is not so easy or evident. Technically, for a control to accept other
controls inside it at design-time (selectable controls, using drag and drop,
etc. like a GroupBox), its designer must be the
System.Windows.Forms.Design.ParentControlDesigner class, or a derived one.

I am not sure if you know about the design-time architecture, designers,
attributes and so on, so I have written the whole function that I think that
you are looking for:

' Add the System.Design.dll assembly to your project's references !!!

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim ctlGroupBox As New GroupBox()
Dim ctlTabControl As New TabControl()
Dim ctlTextBox As New TextBox()

IsContainerControl(ctlGroupBox)
IsContainerControl(ctlTabControl)
IsContainerControl(ctlTextBox)

End Sub

Public Function IsContainerControl(ByVal ctlControl As Control) As Boolean

Dim bResult As Boolean = False
Dim objControlType As Type
Dim objDesignerType As Type
Dim objAttribute As Attribute
Dim objDesignerAttribute As System.ComponentModel.DesignerAttribute

objControlType = ctlControl.GetType
For Each objAttribute In
objControlType.GetCustomAttributes(GetType(System.ComponentModel.DesignerAttribute),
False)

If TypeOf objAttribute Is System.ComponentModel.DesignerAttribute Then
objDesignerAttribute = CType(objAttribute,
System.ComponentModel.DesignerAttribute)
objDesignerType =
System.Type.GetType(objDesignerAttribute.DesignerTypeName)
If
GetType(System.Windows.Forms.Design.ParentControlDesigner).IsAssignableFrom(objDesignerType)
Then
bResult = True
Exit For
End If
End If

Next

MessageBox.Show(objControlType.FullName & " is a container control: " &
bResult.ToString)

Return bResult

End Function




--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
Carlos said:
Hi Tom,

It is not so easy or evident. Technically, for a control
to accept other controls inside it at design-time
(selectable controls, using drag and drop, etc. like a
GroupBox), its designer must be the
System.Windows.Forms.Design.ParentControlDesigner class,
or a derived one.

I am not sure if you know about the design-time
architecture, designers, attributes and so on, so I have
written the whole function that I think that you are
looking for:

' Add the System.Design.dll assembly to your project's
references !!!

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load

Dim ctlGroupBox As New GroupBox()
Dim ctlTabControl As New TabControl()
Dim ctlTextBox As New TextBox()

IsContainerControl(ctlGroupBox)
IsContainerControl(ctlTabControl)
IsContainerControl(ctlTextBox)

End Sub

Public Function IsContainerControl(ByVal ctlControl As
Control) As Boolean

Dim bResult As Boolean = False
Dim objControlType As Type
Dim objDesignerType As Type
Dim objAttribute As Attribute
Dim objDesignerAttribute As
System.ComponentModel.DesignerAttribute

objControlType = ctlControl.GetType
For Each objAttribute In
objControlType.GetCustomAttributes(GetType(System.Componen
tModel.DesignerAttribute), False)

If TypeOf objAttribute Is
System.ComponentModel.DesignerAttribute Then
objDesignerAttribute = CType(objAttribute,
System.ComponentModel.DesignerAttribute)
objDesignerType =
System.Type.GetType(objDesignerAttribute.DesignerTypeName)
If
GetType(System.Windows.Forms.Design.ParentControlDesigner)
.IsAssignableFrom(objDesignerType) Then
bResult = True Exit For End If
End If

Next

MessageBox.Show(objControlType.FullName & " is a
container control: " & bResult.ToString)

Return bResult

End Function

Carlos: Thanks! Looks good, I'll check this out.

--
 

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

Back
Top