Page 1 of 1

Dynamic hierarchy

Posted: Sunday 8th August 2021 12:07am
by Technopeasant
I am wondering if there is any way to change the hierarchy of controls in code. The only way I know how is to generate the objects in code in the order you want them to be placed. This is annoying when you would want a control created from within the IDE to be above a control generated from within the program (i.e. the HUD elements in my new game Bring Them On). I could just generate the labels in code, but that does not seem like the optimum solution.

Re: Dynamic hierarchy

Posted: Sunday 8th August 2021 1:24am
by BruceSteers
Technopeasant wrote: Sunday 8th August 2021 12:07am I am wondering if there is any way to change the hierarchy of controls in code. The only way I know how is to generate the objects in code in the order you want them to be placed. This is annoying when you would want a control created from within the IDE to be above a control generated from within the program (i.e. the HUD elements in my new game Bring Them On). I could just generate the labels in code, but that does not seem like the optimum solution.
MyControlName.Raise() will bring it to the top. .Lower() to the bottom for your base objects

Re: Dynamic hierarchy

Posted: Sunday 8th August 2021 11:20am
by cogier
You can also alter the order in the IDE. Below, I have reversed the order of 3 Buttons.

Image

Re: Dynamic hierarchy

Posted: Wednesday 11th August 2021 3:44am
by Technopeasant
I knew there had to be something obvious like that...

Thanks Bruce!

Would be kinda nice to be able to re-order controls in relation to each other and not just the parent, but it does the job.

EDIT: I am now curious about the Hide method. How is it different than changing the visibility?

Re: Dynamic hierarchy

Posted: Wednesday 11th August 2021 10:32pm
by Quincunxian
To re-order the controls you will need to have them in a container - A Panel is well suited to this.
I use something similar to set tool buttons in a horizontal panel - This example is set for a vertical sequence.
{As per your example}
If you want to centre them in the panel, you can use
Ctrl.X = (InPanel/2) - (InW/2)


Because you are using the 'For Each' in the subroutine, it will use the sequence set in the hierarchy to process.
Private Sub SetButtonSequence(InH As Integer, InW As Integer, ByRef InPanel As Panel)
Dim Ctrl As Control 
Dim Buffer As Integer = 5
Dim TmpPos As Integer = Buffer
 
For Each Ctrl In InPanel.Children
  If Ctrl Is Button Then
    Ctrl.Height = InH
    Ctrl.Width = InW
    Ctrl.X = Buffer
    Ctrl.Y = TmpPos
    TmpPos += (InH + Buffer)
  Endif 
Next