Start position of ChildForm relative to ParentForm

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
MarvelManiac
Posts: 63
Joined: Mon May 01, 2017 8:28 pm

Start position of ChildForm relative to ParentForm

Post by MarvelManiac » Fri Feb 09, 2018 9:13 am

When I click on a label, it brings up my child form. The child form has two images.

What I would like is the child form, to always show in the exact location relative to the Parent no matter where the Parent is located
ChildParentLocation.png
ChildParentLocation.png (23.7 KiB) Viewed 714 times
I can set the Start Position of the Child to CenterParent but it'd look much better, if it was always directly above the label

User avatar
jvierra
Posts: 12883
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Start position of ChildForm relative to ParentForm

Post by jvierra » Fri Feb 09, 2018 9:40 am

Just set the location of the form to the label upper-left corner.

User avatar
MarvelManiac
Posts: 63
Joined: Mon May 01, 2017 8:28 pm

Re: Start position of ChildForm relative to ParentForm

Post by MarvelManiac » Fri Feb 09, 2018 9:51 am

The $label from the main form appears it cannot be called in the childform
labellocation.png
labellocation.png (22.04 KiB) Viewed 701 times

User avatar
jvierra
Posts: 12883
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Start position of ChildForm relative to ParentForm

Post by jvierra » Fri Feb 09, 2018 10:09 am

You have to pass the location to the child form. The child cannot access a parent by any normal means.

In the form load you can also try this:

$this.Location = $this.Parent.Controls['label1'].Location

I suspect you will have to calculate the screen relative location referenced to the parent form and the label.

User avatar
MarvelManiac
Posts: 63
Joined: Mon May 01, 2017 8:28 pm

Re: Start position of ChildForm relative to ParentForm

Post by MarvelManiac » Fri Feb 09, 2018 5:33 pm

Thanks. I’ll report back any findings.

User avatar
jvierra
Posts: 12883
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Start position of ChildForm relative to ParentForm

Post by jvierra » Fri Feb 09, 2018 6:24 pm

Here is project that demonstrates how to position a child relative to a parent.
Attachments
Test-ChildPosition.zip
(102.33 KiB) Downloaded 20 times

User avatar
MarvelManiac
Posts: 63
Joined: Mon May 01, 2017 8:28 pm

Re: Start position of ChildForm relative to ParentForm

Post by MarvelManiac » Sun Feb 11, 2018 11:05 pm

I was surprised how small of increments my height and width needed to change. But we have success. Some brilliant work! Thank you.
Winner.png
Winner.png (20.54 KiB) Viewed 589 times

Locked