Create Control at Runtime

Xojo 2021 R 3.1
Mac
Mojave

I have created controls at Runtime using ControlSets in the past. Now the documentation tells me that I can create a control at runtime. How is this done? I can find the documentation describing how to do it using Control Sets but what is this better way to do it? I have not found the documentation for creating a Control at Runtime without using a ControlSet

From the documentation:

Control Sets are no longer needed because you can now create controls at runtime and add them to the layout for all project types. It is suggested that you do that rather than use Control Sets.

I have tried various things with no success. One, just to prove I have tried, was to have a property of a window be oneRect and in the code of the Opening event of the window

oneRect = New DesktopRectangle
oneRect.Parent = Self
oneRect.Left = 500
oneRect.Top = 600
oneRect.Width = 100
oneRect.Height = 80
oneRect.AllowAutoDeactivate = True
oneRect.BorderColor = KColor.BLACK
oneRect.BorderThickness = 1 
oneRect.FillColor = KColor.BLUEBERRY
oneRect.CornerSize = 16
oneRect.Transparent = False
oneRect.Visible = True

I had hoped that there would be a rectangle on the window when it appeared but nothing. Various others things I tried did not work. So what is the way that you code a Control to appear at runtime?

KColor.BLACK and KColor.BLUEBERRY are just constants that I have in my code.

You need to add the control to the window using AddControl:
https://documentation.xojo.com/api/user_interface/desktop/desktopwindow.html.AddControl

1 Like
Var oneRect As New DesktopRectangle
oneRect.Left = 500
oneRect.Top = 600
oneRect.Width = 100
oneRect.Height = 80
oneRect.BorderColor = KColor.BLACK
oneRect.BorderThickness = 1 
oneRect.FillColor = KColor.BLUEBERRY
oneRect.CornerSize = 16

Self.AddControl(oneRect)
1 Like

Hi @Robert_Livingston

Also, from the blog:

1 Like

I credit Adam with the solution because he was first but I appreciate the next two posters as well for additional information. All appreciated.

I do like the references to the blog pages. I would comment that in its current state, this information is hard (? impossible) to find in the regular documentation for Desktop. Where the current documentation comments that ControlSets are not necessary, it would be nice to have it go further and link you to a page or explain the AddControl command for Desktop.

1 Like

FYI: the link to → Simplified: Adding User Interface Controls at Runtime

Related: Read “[Simplified: Adding User Interface Controls at Runtime](simplified: Adding User Interface Controls at Runtime)“

In the text of the Desktop: Adding Controls at Runtime is broken. The link provided in the post from Menendez works.

That is a totally gratuitous and ridiculous statement which should be removed from the documentation. Control Sets are quite useful completely apart from the question of whether they are created at runtime or not.

It smacks of a programmer who’s all excited about having created something and now feels compelled to insist on everyone else’s use of it.

2 Likes

Agreed. I have never used control sets to add controls in runtime. But I use a lot of control sets at design time.

1 Like

That has been my major use of them over the years as well… I had perviously commented on how misguided that statement in the docs is IMO in another thread…

When I first saw it I worried that they might be headed towards deprecating control sets.

-Karen

1 Like