Relocate child form after opening and resizing

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.
This topic is 1 year and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Relocate child form after opening and resizing

Post by gitpowershelluser »

I know I can set the childform to open on CenterParent. However, during my script if a variable is true, I need to resize the already open ChildForm it to accommodate the new objects on the screen.

How would I reset the child location to the center of parent after resizing?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Relocate child form after opening and resizing

Post by jvierra »

Reset to upper right location to the computed offset center of the screen.

This is best done in the resizing. You can resize and reposition in one line of code not including the arithmetic.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Relocate child form after opening and resizing

Post by jvierra »

Simple example:
  1.     $xpos = 100 # computed new X position
  2.     $ypos = 100 # computed new Y position
  3.     # the following moves the form to the new location
  4.     $form1.Location = [System.Drawing.point]::new($xpos, $ypos)
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Re: Relocate child form after opening and resizing

Post by gitpowershelluser »

Yes Sure, that does it...

but what happens when my main form is not in it's original place? I expect those coordinates to be way off..
I have seen some post regarding carrying an object variable ( $label ) during the show-childform execution but I don't understand what's happening
cheers.
  1.  $p = $label.Parent.PointToScreen($label.Location)
  2.     $p.X -= ($formChildForm.Size.Width/2) - ($label.Size.Width/2)
  3.     $p.Y -= $formChildForm.Size.Height
  4.     $formChildForm.Location = $p
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Re: Relocate child form after opening and resizing

Post by gitpowershelluser »

jvierra wrote: Thu May 05, 2022 3:55 pm Reset to upper right location to the computed offset center of the screen.

This is best done in the resizing. You can resize and reposition in one line of code not including the arithmetic.
I have read many of your posts so I know when you say something you are sure of it.. One line worked for me once I started simple and used the debugger.

#Move the form after resize
$p = $Bannerlabel.Parent.PointToScreen($Bannerlabel.location)
$childform.Location = "$($p.x+110),$($p.y+30)"

Tested this method on my 4k and native laptop 1080p
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Relocate child form after opening and resizing

Post by jvierra »

As I noted above, you have to do the arithmetic and use the current values for the screen and the form. Remember arithmetic? It was that class you slept through in elementary school. Time to remember how to add, subtract, multiply and divide.

Get the width and height of bot screen and form and computer the new X and Y locations.
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Re: Relocate child form after opening and resizing

Post by gitpowershelluser »

Why are you being rude? You know nothing about me.
Did you even read the part where I solved my own problem or did you just sleep through that? What about the part where I said I've read your posts and tried to show appreciation.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Relocate child form after opening and resizing

Post by jvierra »

If it works, then use it although it won't work on every screen aspect ratio which is why you have to use constants.

There are more ways to do things poorly and only a very few ways to do things correctly. Poor methods eventually fail.

If you are satisfied, then OK.
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Re: Relocate child form after opening and resizing

Post by gitpowershelluser »

You'll need more than a bunch of words to prove your point to me to say that I'll fail when you're just assuming. I know when I point fingers there are three pointing back.

Please be kind and be helpful or sit in the corner.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Relocate child form after opening and resizing

Post by jvierra »

Not assuming. I have been building Windows applications for almost four decades.

Don't worry. If you are happy then you may eventually learn when things fail. For the two test you made you are OK. If it fails on the third different screen, then consider using arithmetic and screen/form sizes to set the location to relative center.
This topic is 1 year and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked