I am clueless right now on how I can set the position of a form to open up directly below the parent form, as if the new form is attached directly below it's parent form. The parent form's property is currently set to start at the CenterScreen. I don't like to just ask for some script, but can anyone provide a direction or idea for me?

Dani AI

Generated

A compact, robust pattern for opening a child form directly beneath its parent (works even when the parent was started with CenterScreen). was on the right track about using the parent's location; 's fixed coordinates will work only in a locked environment and are brittle across resolutions. The example below sets the child's StartPosition to Manual, computes a centered X under the parent, keeps the child inside the screen WorkingArea, and assigns the owner so the child stays above its parent.

Dim child As New ChildForm()
child.StartPosition = FormStartPosition.Manual

' center horizontally under parent
Dim x As Integer = Me.Left + (Me.Width - child.Width) \ 2
Dim y As Integer = Me.Top + Me.Height

Dim scr As Screen = Screen.FromControl(Me)
Dim wa As Rectangle = scr.WorkingArea

If x < wa.Left Then x = wa.Left
If x + child.Width > wa.Right Then x = wa.Right - child.Width

If y + child.Height > wa.Bottom Then
    ' not enough room below: show above parent
    y = Me.Top - child.Height
    If y < wa.Top Then y = wa.Top
End If

child.Location = New Point(x, y)
child.Owner = Me
child.Show()

Notes and pitfalls: compute placement after the parent has been shown (Shown is safest), especially if StartPosition is used. To keep the child “attached” while the parent moves or resizes, handle the parent’s Move and Resize events and re-run the placement logic. For modal behavior prefer child.ShowDialog(Me). Always test on different resolutions and multi-monitor setups; using Screen.WorkingArea avoids covering the taskbar or placing the form off-screen.

Recommended Answers

All 2 Replies

Between the instantiation of the child form and the call to the show you can set the top and the left properties of the form. The top would be approximately the sum of the top-of-the-parent- form and the height-of-the-parent-form.

I guess that the form's top and height are available, otherwise you need to pass it to the object/method that opens the child.

I guess you will do something similar with the left. If you keep everythign centered, you won't need to do anything.

Hope this helps.

Ok, I found a cheasy solution. Not the best approach, but since this program is being used in a so-called control enviornment(five laptops set the exact same way), I just figured out the x & y coordinates for where I want the child form and set it to open at that location, thus directly below the parent form.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.