Dynamically generated Textboxes don't load as Variables

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
matthmsft
Posts: 16
Joined: Mon Apr 17, 2017 7:11 pm

Dynamically generated Textboxes don't load as Variables

Post by matthmsft » Thu Aug 15, 2019 11:37 pm

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?

User avatar
Alexander Riedel
Posts: 7023
Joined: Tue May 29, 2007 4:43 pm

Re: Dynamically generated Textboxes don't load as Variables

Post by Alexander Riedel » Thu Aug 15, 2019 11:51 pm

Thank you for posting. Regarding "are not appearing in the same way", I am not sure what that means. Appear where? In the designer?
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
matthmsft
Posts: 16
Joined: Mon Apr 17, 2017 7:11 pm

Re: Dynamically generated Textboxes don't load as Variables

Post by matthmsft » Fri Aug 16, 2019 1:26 am

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?

User avatar
Alexander Riedel
Posts: 7023
Joined: Tue May 29, 2007 4:43 pm

Re: Dynamically generated Textboxes don't load as Variables

Post by Alexander Riedel » Fri Aug 16, 2019 1:43 am

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
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
matthmsft
Posts: 16
Joined: Mon Apr 17, 2017 7:11 pm

Re: Dynamically generated Textboxes don't load as Variables

Post by matthmsft » Sun Aug 18, 2019 7:29 pm

ah, understand!

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