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.
Locked
User avatar
thromada
Posts: 5
Joined: Thu May 28, 2009 1:40 am

Change location of an object at run-time

Post by thromada » Tue Apr 30, 2019 5:20 pm

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: 7096
Joined: Tue May 29, 2007 4:43 pm

Re: Change location of an object at run-time

Post by Alexander Riedel » Tue Apr 30, 2019 5:39 pm

[Topic moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.

jvierra
Posts: 13881
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Change location of an object at run-time

Post by jvierra » Tue Apr 30, 2019 7:25 pm

See the attached file for how to move a label.
Attachments
Test-MoveLabel.psf
(35.47 KiB) Downloaded 24 times

jvierra
Posts: 13881
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Change location of an object at run-time

Post by jvierra » Tue Apr 30, 2019 7:57 pm

Now try this version to get info on how controls can work.
Attachments
Test-MoveLabel2.psf
(35.68 KiB) Downloaded 27 times

User avatar
thromada
Posts: 5
Joined: Thu May 28, 2009 1:40 am

Re: Change location of an object at run-time

Post by thromada » Wed May 01, 2019 7:37 am

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: 13881
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Change location of an object at run-time

Post by jvierra » Wed May 01, 2019 8:17 am

It works fine for me when setting the location in the load event.
Attachments
Test-MoveLabel2.psf
(35.93 KiB) Downloaded 26 times

User avatar
thromada
Posts: 5
Joined: Thu May 28, 2009 1:40 am

Re: Change location of an object at run-time

Post by thromada » Wed May 01, 2019 10:29 am

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.

Locked