window.Controls iterator - Illegal cast?

Im trying rearrange some buttons and im using this new iterator from the LR:

For Each c As DesktopControl In Self.Controls
  If c IsA mySubClass Then
    // do my thing
  End If
Next

and its throwing an illegal cast exception on a pagePanel. The LR is pretty short and clear this should return DesktopControls so Im a bit stumped. I tried just ‘Control’ as the cast but also a no go. Can anyone spot my problem?

A PagePanel cannot be cast to a DesktopControl, it can however be cast to a Control, you’ll need to change the type after the As or change the panel to a DesktopPagePanel or change the type after the As to Variant

Thanks Julian, I tried that and it still threw the illegal cast but this time on a null value for one of the 'c’s

Julian - Variant works of course but I still dont believe whats in the LR doesnt work. The panel is a DesktopPagePanel. I cant quite figure out what was coming in as nil

This seems to work in my quick test for both DesktopPagePanel and PagePanel

For Each c As Variant In Self.Controls
  If c IsA mySubClass Then
    // do my thing
    Break
  End If
Next

Pop up a quick test binary if you want and I’ll take a look.

1 Like

Which doc page are you looking at?

In my local LR, “DesktopWindow.Controls” method. I am running 2021r3.1. The “Notes” section says all controls on the window are returned as DesktopControls but not the panel and then the nil? I’ll see if I can just unwind what im doing here into a binary…it works with variant but thats kind of bodge it seems to me.

Ok, both As ‘DesktopControl’ or ‘Control’ work in a blank project with roughly the same setup as in my app so its something to do with my meddling I guess. I wont bore you with the working binary and Im not going to spend any more time on it since it somehow seems to be self inflicted.

If it’s a PagePanel, it breaks, but if the panel have a Super DesktopPagePanel, it works for a

For Each c As DesktopControl In Self.Controls