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 LICENSE NUMBERS, ACTIVATION 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.
This topic is 4 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.
User avatar
matthmsft
Posts: 16
Last visit: Fri Oct 02, 2020 12:31 am

Dynamically generated Textboxes don't load as Variables

Post 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?
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Dynamically generated Textboxes don't load as Variables

Post 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?
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
matthmsft
Posts: 16
Last visit: Fri Oct 02, 2020 12:31 am

Re: Dynamically generated Textboxes don't load as Variables

Post 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?
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Dynamically generated Textboxes don't load as Variables

Post 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
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
matthmsft
Posts: 16
Last visit: Fri Oct 02, 2020 12:31 am

Re: Dynamically generated Textboxes don't load as Variables

Post by matthmsft »

ah, understand!

Put this one down to inexperience, thanks so much for your help
This topic is 4 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.