Asked by:
Variable not loading

Question
-
Hi,
I'm trying to get the textbox to display the first line of the File,
However when I click each button the text file has “System.Management.Automation.PSVariable”
Any Help?
$form=New-Object System.Windows.Forms.Form function function-to-run{ $search = $This.text $Email = Get-Variable -Name Email$search $textUpdate.Text = $Email } $i=0 $files = (Get-ChildItem H:\People\*.txt).BaseName foreach ($file in $files) { $b=New-Object System.Windows.Forms.Button $b.Name=$file $b.location="0,$(25*++$I)" $b.size='100,20' $b.Text="$file" $b.Tag=$file $b.add_click({ function-to-run }) Set-Variable -name Email$file -Value $file $form.controls.add($b) } $textUpdate = New-Object Windows.Forms.TextBox $textUpdate.Location = New-Object Drawing.Point 0,0 $textUpdate.Size = New-Object Drawing.Point 250,30 $textUpdate.Text = "" $form.controls.add($textUpdate) $form.showdialog()
Thank you!
Wednesday, March 4, 2020 11:31 AM
All replies
-
This:
$textUpdate.Text = $Email
Needs to be this:
$textUpdate.Text = $Email.Value
\_(ツ)_/
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Thursday, March 5, 2020 8:41 AM
Wednesday, March 4, 2020 12:20 PM -
This would make more sense:
$i=0 $files = (Get-ChildItem c:\temp\*.txt).BaseName $form = New-Object System.Windows.Forms.Form $form.Size = '650,400' $form.StartPosition = 'CenterScreen' $buttonClick = { $search = 'Email' + $this.Text $textUpdate.Text = Get-Variable -Name $search -ValueOnly } foreach ($file in $files) { $b = New-Object System.Windows.Forms.Button $form.controls.add($b) $b.Name = $file Write-Host $file -fore green $b.location = "0,$( (25 * $i++) + 25 )" $b.size = '100,20' $b.Text = $file $b.Tag = $file $b.add_click($buttonClick) Set-Variable -name "Email$file" -Value $file } $textUpdate = New-Object Windows.Forms.TextBox $textUpdate.Width = 200 $form.controls.add($textUpdate) $form.showdialog()
Using a FlowLayoutPanel would also simplify this.
\_(ツ)_/
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Thursday, March 5, 2020 8:41 AM
Wednesday, March 4, 2020 12:23 PM -
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
LeePlease remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.Thursday, March 5, 2020 8:41 AM