Please help with InitialParent

There are overlapping controls in my windows. Some work correctly. Some don’t. Examining the XML of my Xojo project, the difference is the InitialParent value.

Xojo removed InitialParent from the Inspector in 2014. So how do we set the InitialParent value without editing the raw XML?

Any suggestions?

You can see the rules for how a control’s parent gets set in the Notes section here: https://documentation.xojo.com/api/user_interface/desktop/desktopuicontrol.html.Parent

If the control is on the window, the Parent will be the Window. If the control is on the container, the Parent will be the container. If the control is completed enclosed by another control, the Parent will be that control.

If you just want the control’s parent to be the window, set its parent property to nil.

me.parent = nil

Also, if you need to be in a parent but moved off it, you can move the control into the parent with the mouse then move it by altering the Left and Top values in the inspector, this will keep its parent but allow you to move it outside of the parent, which you can’t do when moving it with the mouse.

You can check the parent of a control in the IDE by selecting it and seeing where the red outline is, if you want to unparent it for whatever reason, you have to move the control back into the parent (see above) then you can right click it and select “Unparent”

Thanks, Gavin and Julian. The problem is solved :slight_smile:

In the Window “open” event handler, we can change the InitialParent value using:
childcontrol.parent = parentcontrol (or Nil)

In a test situation, this improved the appearance and the event handling:
me.TestRect.parent = nil // custom rectangle
me.TestLabel.Parent = TestRect // overlapping label
me.TestCanvas.parent = TestRect // overlapping canvas

I couldn’t see the right-click, “Unparent”, option in 2021R3.1

But this is not enough. To get the controls to display correctly, I had to rearrange the code for the overlapping controls in the XML project file. The order in the project XML needed to be:
TestRect
TestLabel
TestCanvas

This is not the same as their order in the IDE list. Is there any easy way to reorder controls in the XML, other than cut-and-paste?

The order that the controls are saved in the xml file is directly related to the z-order of the controls on the window (at least that’s what it shows after a quick test) so those further back are placed to the top of the xml file’s relevant section. You are correct, this isn’t the order that they are shown in the Navigator. You can change the z-order by clicking the forward/front/backward/back icons just above the form designer. Unfortunately, there is no visualiser for this or quick drag method.

If you can’t see the parent indicator or the “Unparent” menu option then it probably has no parent. As I mentioned above, it is possible to have a control look like it should be parented but it isn’t, this is done by moving it either by changing its Top/Left properties or by resizing it in the IDE. If you click TestLabel and it doesn’t show RestRect with a red border then it doesn’t have a parent.

Side note, you shouldn’t be able to move the z-order of a child control above its parent in the IDE. The activation of the ordering buttons depends on the number of controls in the same “container” so ones on the window would be separate to controls children to a canvas.

2 Likes

Thanks, Julian. I’ve never noticed the z-order buttons or how the red box works. Much appreciated :smile:

1 Like