dynamically move component

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 10 years and 7 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
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

dynamically move component

Post by IanUoY »

Quite simply I wanted to move a label around my form to advise a user that a particular text box requires entry. I was trying the following and variations but cannot get anything to work:-

$labelTextRequired.Location = System.Drawing.Point(106,84)

Regards

Ian
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: dynamically move component

Post by jvierra »

Almost but this is PowerShell and constructors are not automatically called.
PowerShell Code
Double-click the code block to select all.
$labelTextRequired.Location = New-Object System.Drawing.Point(106,84)
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Re: dynamically move component

Post by IanUoY »

Brilliant....almost :-)

I think the co-ordinates I am using relate to the relative position on the panel I've grouped my components onto, so when I apply the code, it's in the wrong position. Appears to be relative to the form. Can I reference the panel object doing this?

I presume this would then use the backcolor of the panel control properly.

Cheers

Ian
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: dynamically move component

Post by jvierra »

What panel object. I see no panel object.

Have you thought of doing this like everyone else. I know it sounds boring but the norm would be to flash the color to red and back a few times.
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Re: dynamically move component

Post by IanUoY »

The component in question sits on a panel object. The location of my example textbox indicates it's location is at 106,64....so my idea was to simply move a label to just underneath the textbox. The label is red and has simple text of "* required" and I set the focus back to the textbox in question if nothing has been entered.

When I use the code given, the label appears higher up and to the left, making me think the co-ordinates are referencing the form, and additionally the backcolor is the same as the form (not the panel) and the backcolor is currently set to control.

Cheers

Ian
User avatar
IanUoY
Posts: 91
Last visit: Wed Sep 20, 2023 2:44 am

Re: dynamically move component

Post by IanUoY »

Sorted in one of two ways (although if anyone else could advise how to reference an object, great).

MY first clunky way was to:-
#$labelTextRequired.Location = New-Object System.Drawing.Point(($panel1.Location.X + $textboxForename.Location.x),($panel1.Location.y + $textboxForename.Location.y + 20))

So I was referring to the panel location and adding as required.

My second way was quite simply to have the label exist on the panel which also resolved the background colour. Previously I had moved it away to the edge of the form and made "invisible", but of course then it was not associated with the panel. So by having it on the panel, the code given previously:-

$labelTextRequired.Location = New-Object System.Drawing.Point(106,84)

worked fine.

Ian
This topic is 10 years and 7 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