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

**URL:** <https://forum.xojo.com/t/how-to-dynamically-add-member-to-control-set-in-api-2-xojo-2021r3-1/67884>\
**Category:** General\
**Created:** [January 10, 2022, 12:06pm UTC](https://forum.xojo.com/t/how-to-dynamically-add-member-to-control-set-in-api-2-xojo-2021r3-1/67884 "2022-01-10T12:06:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_Sun](https://forum.xojo.com/user_avatar/forum.xojo.com/frank_sun/32/1482_2.png) [@Frank\_Sun](https://forum.xojo.com/u/Frank_Sun)\
**Post date:** [January 10, 2022, 12:06pm UTC](https://forum.xojo.com/t/how-to-dynamically-add-member-to-control-set-in-api-2-xojo-2021r3-1/67884/1 "2022-01-10T12:06:22Z")

</div>

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

```auto
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

```auto
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.

> **[Desktop: Adding Controls at Runtime – Xojo Programming Blog](https://blog.xojo.com/2022/01/07/desktop-adding-controls-at-runtime/)**

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

# DesktopWindow.AddControl

### From Xojo Documentation

Method

**[DesktopWindow](https://documentation.xojo.com/api/user_interface/desktop/desktopwindow.html).AddControl**(control as [DesktopUIControl](https://documentation.xojo.com/api/user_interface/desktop/desktopuicontrol.html))

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](https://documentation.xojo.com/topics/user_interface/desktop/desktop_controls/control_sets.html)

Would anyone know the correct method to use?

Thanks,  
Frank

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [January 10, 2022, 12:12pm UTC](https://forum.xojo.com/t/how-to-dynamically-add-member-to-control-set-in-api-2-xojo-2021r3-1/67884/2 "2022-01-10T12:12:17Z")

</div>

Is **MAK95Window** of type `DesktopWindow`?

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [January 10, 2022, 12:24pm UTC](https://forum.xojo.com/t/how-to-dynamically-add-member-to-control-set-in-api-2-xojo-2021r3-1/67884/3 "2022-01-10T12:24:20Z")

</div>

> [@Frank\_Sun](#):
>
> ```auto
> Var newPBB As BevelButton
> 
> ```

Perhaps this should be:

> [@](#):
>
> ```auto
> Var newPBB As PaperBevelButton
> 
> ```

---

<div class="post-metadata">

**Author:** ![Frank\_Sun](https://forum.xojo.com/user_avatar/forum.xojo.com/frank_sun/32/1482_2.png) [@Frank\_Sun](https://forum.xojo.com/u/Frank_Sun)\
**Post date:** [January 10, 2022, 8:02pm UTC](https://forum.xojo.com/t/how-to-dynamically-add-member-to-control-set-in-api-2-xojo-2021r3-1/67884/4 "2022-01-10T20:02:49Z")

</div>

Hi,

Yes MAK95Window is a DesktopWindow.

I just tried your suggestion:

```auto
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

```auto
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…

---

<div class="post-metadata">

**Author:** ![anon20074439](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/ac8455/32.png) [@anon20074439](https://forum.xojo.com/u/anon20074439)\
**Post date:** [January 10, 2022, 10:37pm UTC](https://forum.xojo.com/t/how-to-dynamically-add-member-to-control-set-in-api-2-xojo-2021r3-1/67884/5 "2022-01-10T22:37:35Z")

</div>

Your initial issue with the wrong type has been reported \<[https://xojo.com/issue/67234](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.

---

<div class="post-metadata">

**Author:** ![Frank\_Sun](https://forum.xojo.com/user_avatar/forum.xojo.com/frank_sun/32/1482_2.png) [@Frank\_Sun](https://forum.xojo.com/u/Frank_Sun)\
**Post date:** [January 11, 2022, 1:08am UTC](https://forum.xojo.com/t/how-to-dynamically-add-member-to-control-set-in-api-2-xojo-2021r3-1/67884/6 "2022-01-11T01:08:55Z")

</div>

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
