How to dynamically add member to control set in API 2 xojo 2021r3.1

Previously, when I was using BevelButton, this code used to work:

Var newPBB As BevelButton
newPBB = New PaperBevelButton //this is a control set of type BevelButton
newPBB.Top = PaperBevelButton(index-1).Top + RowHeight

However, since changing everything to use the new API 2 DesktopUIControls, the equivalent code throws an IllegalCastException on line 2

Var newPBB As DesktopBevelButton
newPBB = New PaperBevelButton //super of this control set is now DesktopBevelButton, throws exception
newPBB.Top = PaperBevelButton(index-1).Top + RowHeight

The message of the exception is

MAK95Window.MAK95Window cannot be cast to PaperAnalysisListboxCanvasContainer.PaperAnalysisListboxCanvasContainer

PaperBevelButton is a child of PaperAnalysisListboxCanvasContainer which is a child of the window MAK95Window. So I am very confused as to what is happening…

Is it still possible to dynamically create new members of a control set at runtime in API 2? I read a recent blog post saying that there is a new way of adding controls, AddControl.

However, I can’t find any information on how to implement AddControl

DesktopWindow.AddControl

From Xojo Documentation

Method

DesktopWindow.AddControl(control as DesktopUIControl)

New in 2021r3

Supported for all project types and targets.

Adds the control passed to the layout.

The LR on Desktop Control Sets seems to suggest that control sets should still work with the new DesktopUIControls.
https://documentation.xojo.com/topics/user_interface/desktop/desktop_controls/control_sets.html

Would anyone know the correct method to use?

Thanks,
Frank

Is MAK95Window of type DesktopWindow?

Perhaps this should be:

Hi,

Yes MAK95Window is a DesktopWindow.

I just tried your suggestion:

Var newPBB As PaperBevelButton
newPBB = New PaperBevelButton

This compiles, but has the same runtime IllegalCastException with the same message. One of the examples given on the Control Set LR is

Var b As DesktopButton // this is the control class
b = New Button1 // create clone of the control on the layout
b.Caption = "Clone"
b.Left = Me.Left + Me.Width + 10 // reposition it

so it seems like we should be defining the variable as the parent class…

Your initial issue with the wrong type has been reported <https://xojo.com/issue/67234> and should hopefully be in the next release.

But you’ve found another nice bug there, control sets aren’t being added to a containers correctly, that will need reporting. The exact same code outside of a container works ok.

What a rabbit hole, the included Control Set example doesn’t even run out of the box.

What a mess.

Thanks for letting me know. At least I feel better that there is an actual bug that’s been identified – rather than it being me who has made some silly mistake!

Cheers,
Frank