Change location of an object at run-time

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 4 years 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
User avatar
thromada
Posts: 5
Last visit: Mon Jul 12, 2021 1:11 pm

Change location of an object at run-time

Post by thromada »

Product, version and build: PowerShell Studio 2018 version 5.5.154
32 or 64 bit version of product: x64
Operating system: Windows 10 1709
32 or 64 bit OS: x64

I have a form on which a label has been drawn. When I call/launch that form, I need to change the location of the label if I need to (based on a variable). Even taking out the variable, if I try to pro-grammatically change the label's location at run-time using code, it just doesn't move.

In the script portion of the form designer, I have tried things like:
  • $label.location.X = 12
  • $label.location.Y = 370
  • $label.location = new-object system.drawing.point(12,370)
  • $label.left = 12
  • $label.top = 370
I've also issued $form.refresh() and $label.refresh().

Nothing code-wise changes the location of that label when the form is called. I've also tried putting those commands inside and outside of the form_load event.

It behaves as if you cannot change the location of an object at run-time. But can you?
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Change location of an object at run-time

Post by Alexander Riedel »

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Change location of an object at run-time

Post by jvierra »

See the attached file for how to move a label.
Attachments
Test-MoveLabel.psf
(35.47 KiB) Downloaded 153 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Change location of an object at run-time

Post by jvierra »

Now try this version to get info on how controls can work.
Attachments
Test-MoveLabel2.psf
(35.68 KiB) Downloaded 135 times
User avatar
thromada
Posts: 5
Last visit: Mon Jul 12, 2021 1:11 pm

Re: Change location of an object at run-time

Post by thromada »

These are great, thank you. I had an Easter egg in an old MS Access app that would run a game called "Catch the OK Button", similar to your examples.

I was able to duplicate making a label (or any other object) move at run time by using a button event. That is, click a button and the label moved to the location I specified. However, I think I'm not understanding how forms load or open something to that effect, so I'll have to re-specify my question below.

When I open this form I am checking a passed-parameter for a value. If the value is $true, I want the label in a certain location. If the value is $false, I want the label in another location. This should happen at some point so when the form renders, it shows in the appropriate location. However, I cannot seem to get the label to move from it's original location. So how can I make this object move when the form is loaded/opened/called?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Change location of an object at run-time

Post by jvierra »

It works fine for me when setting the location in the load event.
Attachments
Test-MoveLabel2.psf
(35.93 KiB) Downloaded 152 times
User avatar
thromada
Posts: 5
Last visit: Mon Jul 12, 2021 1:11 pm

Re: Change location of an object at run-time

Post by thromada »

I believe my original form I'm using is "corrupted" somehow. When I create a new simple form and call it with the location changes in the load event, it also works. I will redo this all with a new form. Thank you, I appreciate you showing some working examples.
This topic is 4 years 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