If you’ve ever used a container control on a layout and then tried to manipulate the controls at runtime with code like
For i As Integer = 0 To Self.ControlCount - 1 Dim ctrl As Control = Self.Control(i) // now manipulate this control or call its methods Next
you’ve probably run into the EmbeddedWindowControl
Its what you get when you use a Container Control on a layout either by placing one at design time or dynamically adding one at runtime.
Note that you get an EmbeddedWindowControl – not an instance of the Container you placed there. So any code you use that tests if a control ISA specific instance will fail for your containers. You don’t get a reference to the instance you get this EmbeddedWindowControl instead. So you cant call any methods you have added to the container control or reference any properties of it.
And today, for a project I was working on, I needed this and decided to figure out how to make this possible.
The result is this little sample program with one module that extends EmbeddedWindowControl in a way you can get the container control that the embedded window control was created from.
To make use of this simply copy the EmbeddedWindowControlExtensions module into your project. Then when you iterate a set of controls on a layout you can do
For i As Integer = 0 To Self.ControlCount - 1 Dim ctrl As Control = Self.Control(i) If ctrl IsA EmbeddedWindowControl Then Dim cc As ContainerControl = EmbeddedWindowControl(ctrl).Container If cc <> Nil Then // you now have an actual reference to the container control // so you can use its methods, properties, etc // but you will need to cast it first End If End If Next