EmbeddedWindowControl

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.

Its been most annoying. There have been several requests for something to be done so the container the EmbeddedWindowControl was created from can be accessed. Like this one and this one and this one.

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

Enjoy !

2 Replies to “EmbeddedWindowControl”

  1. Thanks for the example project. 🙂

    But why not use EmbeddedWindowControl.handle instead of iterating through all runtime objects to find it?

    1. You have to go through the runtime to find the matching Container Control that was first embedded as Window.Control only gets you the EmbeddedWindowControl. Basically you have to find the ContainerControl that has the same handle as you cant cast the EmbeddedWindowControl to a Container.

      Re read your comment and I see what you were initially asking about. You’re right I do not need to go through the runtime to get the HANDLE value from the initial EmbeddedWindowControl. I can just use the property. Then I can go through the runtime to find the container control instance that has that same handle.

      I’ve updated the example to reflect this good observation as well as to fix up an error about where I would embed the controls in the open event (they would obscure the button on Windows)

      There’s a feature request to make it so this is not necessary http://feedback.xojo.com/case/23030

Comments are closed.