Form Control over Name

Hi everybody,

I’m new hear, but not new in Powershell.

I just started to try a new way with System.Windows.Forms
I gut a pretty nice Form with Dynamically Created Forms.

Now my problem.
I need to access a label using the name the name.
I think i’m just to stupid to do it :frowning:

My Code

Add-Type -AssemblyName System.Windows.Forms

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '492,280'
$Form.text                       = "Simple Management"
$Form.TopMost                    = $true

$Label1                          = New-Object system.Windows.Forms.Label
$Label1.text                     = "Computer name"
$Label1.AutoSize                 = $true
$Label1.width                    = 25
$Label1.height                   = 10
$Label1.location                 = New-Object System.Drawing.Point(29,29)
$Label1.Font                     = 'Microsoft Sans Serif,10'
$Label1.Name                     = "aaaa"
$Label1.AccessibleName           = "bbbb"


I’ve tried a lot, and searched many.

Has someone an idea how to manage this?


You don’t have to do this from scratch.
You can use a GUI editor to create forms.


Yet, doing from scratch is a good learning experience.

As for…

I need to access a label using the name the name.

You do not have anything on your form called ‘Name’. What you have is a label element called ‘$Label1’ that has a property called Name.

What do you want to do with the label element, that you’ve already assigned a property settings and values?

You’d normally put in another form element next to the label element that is associated with it that user will / could interact with. Otherwise you just are changing it’s property settings, color, font family, etc… User don’t interact with labels under normal circumstances. They are just there to give the user some indication of what is or could be enter in the input element next to it.

Yet, remember, we are here to help you with code that is either not working or that you are having other issue with. Not really GUI design stuff. There are lots of sites / resources to educate you on GUI design in PowerShell using WPF/XAML/Web, etc… Heck there are tons of YouTube videos that walk you through all that as well …

and other videos on MS Channel9.

For Example

Hi Postanote,

Thanks for your reply.

The whole program has a configfile where the devices where listed.
Each device get 4 Elements in the Gui, (btnON, btnOFF, btnRESET, lblSTATUS)

The Variable will be each time the same during a Foreach Loop.
That’s why i would access the element over the Name.

The Label will be the Status Display. A background Job will update the status every x seconds or after a action on a device.

Thats why i need to access the objects over a dynamic way.

I will take a look at your links. Thanks

OK, then you are only acting on this …

$Label1.text                     = "Computer name"

… on every pass.

Not really these…

$Label1.Name                     = "aaaa"
$Label1.AccessibleName           = "bbbb"

… since these are not something the user will ever see. These are just elements, that you can get, but you really should not be dynamically changing them. I cannot think of a good reason why you would.

If these …

btnON, btnOFF, btnRESET, lblSTATUS

… never change, then I would just set them once and move on. Updating a status element makes sense.
So, lblSTATUS.Text to whatever as needed.