Page 1 of 1

Dynamically generated Textboxes don't load as Variables

Posted: Thu Aug 15, 2019 11:37 pm
by matthmsft
Product: PowerShell Studio 2019 (64 Bit)
Build: v5.6.167
OS: Windows 10 Enterprise (64 Bit)
Build: v10.0.18362.0

Hi

I am dynamically generating an Options page from XML. My code is as follows:

$OptionsForPage | ForEach-Object {

$label = New-Object System.Windows.Forms.Label
$label.Name = "lbl_$($psitem.Name)"
$label.Text = $psitem.Name
$label.Location = "20, $yControl"
$label.Size = "220,20"
$label.TextAlign = 'MiddleRight'
$panel_ConfigXML.Controls.Add($label)


$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Name = "txt_$($psitem.Name)"
$TextBox.Text = $psitem.Value
$TextBox.Location = "250, $yControl"
$TextBox.Size = "750,20"
$TextBox.TextAlign = 'Left'
$panel_ConfigXML.Controls.Add($TextBox)


$y = $y + 40
$yControl = [System.Convert]::ToString($y)
}

Everything loads and displays correctly and as expected. What is not happening, is I am expecting to be able to reference my Textbox by, for example $txt_MyTextBox, in the same way I would a static control. However, these variables (matching the name of the control) are not appearing in the same way they would with a static control.

Is there anything I need to add/change? Is this expected behavior?

Re: Dynamically generated Textboxes don't load as Variables

Posted: Thu Aug 15, 2019 11:51 pm
by Alexander Riedel
Thank you for posting. Regarding "are not appearing in the same way", I am not sure what that means. Appear where? In the designer?

Re: Dynamically generated Textboxes don't load as Variables

Posted: Fri Aug 16, 2019 1:26 am
by matthmsft
If I have a control like a Textbox explicitly placed on a form, I can access it through a variable that it is the object name, for example if I have a Textbox with the name "MyTextbox", I would be able to change its text through $MyTextBox.Text

When I dynamically generate textboxes on a form in my project as shown in my code, I am not getting access to it through the same variable name as described above. So if my code dynamically generates a textbox on my form with the Name of "Textbox1", I cannot use $textbox1.Text to set the content and in the Variables window in debug mode, I do not see $textbox1, but I do see the statically defined $MyTextBox as in the first example.

Does that help to clarify?

Re: Dynamically generated Textboxes don't load as Variables

Posted: Fri Aug 16, 2019 1:43 am
by Alexander Riedel
Oh. You assumed that the name of the control automatically transfers to the variable name. That is not the case.
The generated code keeps the name of the control and its variable name in sync:
$linkLabel1.Name = "linkLabel1"

That may make it appear that the name is used, but that's not the case. It is simply that the variable name generated is the same as the name you specified.

This here may help you find your controls by their name dynamically. It is C# of course but transfers easily.
https://stackoverflow.com/questions/389 ... s-controls

Re: Dynamically generated Textboxes don't load as Variables

Posted: Sun Aug 18, 2019 7:29 pm
by matthmsft
ah, understand!

Put this one down to inexperience, thanks so much for your help