Referencing a DesktopTextArea (and other controls) in a layout

What is the recommended way in code to refer to the objects in a layout?

I’ll ask by way of an example of two DesktopTextAreas (TA1, TA2) and one button in a layout where the button assigns a color to the DesktopTextArea with the focus like: ApplyRandomColorTo(TAWithFocus).
Each DesktopTextArea assigns TAWithFocus to itself as the DesktopTextArea gets the focus.

The question is, should TAWithFocus be the pointer TA1.Handle?
If so, how are pointers dereferenced? ApplyRandomColorTo would receive VAR inPointer as Ptr.
or
Can objects be referenced by name and TAWithFocus can be a string with the DesktopTextArea’s name?
If so, what is the syntax to use the name?
or
Is there a different approach to this?

Thanks - KC

There are a couple of ways to do this. They’re all based on the fact that you can pass a control directly. The simplest case is where ApplyRandomColor always expects a DesktopTextArea. In this case, you define it as

Sub ApplyRandomColor(TA as DesktopTextArea)

and call it like

ApplyRandomColor(TAWithFocus)
ApplyRandomColor(TA1)
etc.

The second way is to use a common parent class such as DesktopUIControl. Here, you define the method using the parent class, but you need additional logic to determine what kind of control you passed it. You still call it the same way.

Sub ApplyRandomColor(theControl as DesktopUIControl)
   if theControl IsA DesktopTextArea then
      var TA as DesktopTextArea
      TA = DesktopTextArea(theControl)  // cast theControl to the proper type
      // apply color
   elseif theControl IsA DesktopButton then
      var B as DesktopButton = DesktopButton(theControl)  // shorthand version of above
      // apply color
   end if
end Sub

The point is that you can pass a control or a reference to a control (such as TAWithFocus) directly. No need to resolve pointers or names.

could be easier if you just give the window as input into the method.
or see
https://documentation.xojo.com/api/language/paramarray.html

instead of
ApplyRandomColorTo(TA)
maybe you could use a neutral method with just return a color
TA.TextColor = RandomColor()

in Xojo you not need a Pointer or .Handle just the object.

Thanks All,
With TAWithFocus as a DesktopTextArea property of a Module (GLBL), it can be used to reference the TextArea that has the focus. The TextArea’s FocusReceived method has “GLBL.TAWithFocus=Me”.

After that the button could be used to change the TextArea in the Layout.
GLBL.TAWithFocus.Text=“Hello”
GLBL.TAWithFocus.StyledText.Bold(0, 2) = True

But maybe a more self contained approach would be to make the TextArea and Button all one object (a Class?) that returns a TextArea value and then use instances of that (dragged from the Library?) for the text areas in Layouts. Is there some guidance in the Docs to getting things added to the Library, or to making them available to projects? Any keywords or links would be appreciated.

Sounds like you need to create a DesktopContainer. Then you can size it and add controls to it in the IDE, and it will appear as a Project Control in the Library. That can be dragged onto the layout to create an instance, or you can create one in code and add it to the window. One DesktopContainer is allowed to contain instances of another, if the overall layout would benefit. I have a number of these in my app.

1 Like