0

Please Note this is PowerShell 4.0. I don't think Convert-String is available to , but granted, answers for other versions of PowerShell are still worth the read.

Alright. I'm very new to PowerShell, but interestingly enough both through Google and StackOverFlow I haven't been able to find the answer to this; I'm just not sure where to go yet.

Please keep in mind that i want to launch the file example.ps1 from the PowerShell prompt.

Function Show_Form
{
Add-Type -AssemblyName System.Windows.Forms

$global:Y_Position = 10
$global:X_Position = 5

$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Sample Form"
$Form.Width = 400
$Form.Height = 450
$Form.AutoScroll = $True
$Form.AutoSize = $True
$Form.AutoSizeMode = "GrowOnly"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False

$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Please Fill in the Required Information:`n"
$Label.AutoSize = $True
$Form.Controls.Add($Label)

$LabelTextBox = New-Object System.Windows.Forms.Label
$LabelTextBox.Location = New-Object System.Drawing.Point(10,40)
$LabelTextBox.Text = $Text
$LabelTextBox.AutoSize = $True
$Form.Controls.Add($LabelTextBox)

$global:textBox = New-Object System.Windows.Forms.TextBox
$X_Position = $X_Position+90
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$Form.Controls.Add($textBox)


$Okbutton = New-Object System.Windows.Forms.Button 
$Okbutton.Location = New-Object System.Drawing.Size(250,570) 
$Okbutton.Size = New-Object System.Drawing.Size(100,30) 
$Okbutton.Text = "OK"


<#-----------------------------------#>
$global:Arr = @("lll", "ppp", 2)
$OkButton.Add_Click({[System.Windows.Forms.MessageBox]::Show($textBox.text, "My Dialog Box")})
$OkButton.Add_Click({Write-Host $textBox.text})

$global:Arr[0] = $textBox.text
$OkButton.Add_Click({Write-Host $Arr})
<#-----------------------------------#>

$Okbutton.Add_Click({$Form.Close()})
$Form.Controls.Add($Okbutton)


$Form.ShowDialog()
}

Show_Form

My problem is with these lines:

$global:Arr = @("lll", "ppp", 2)
$OkButton.Add_Click({[System.Windows.Forms.MessageBox]::Show($textBox.text, "My Dialog Box")})
$OkButton.Add_Click({Write-Host $textBox.text})  

$global:Arr[0] = $textBox.text
$OkButton.Add_Click({Write-Host $Arr}) 

The Add_Click({Write-Host $textBox.text}) displays in the Console but when I add $textBox.text to an array (object array or otherwise) or replace another element with it then print the Array, it is always just blank.

.

And if I do the following (Note: Arr[0] AND Arr[1])

$global:Arr = @("lll", "ppp", 2)
$OkButton.Add_Click({[System.Windows.Forms.MessageBox]::Show($textBox.text, "My Dialog Box")})
$OkButton.Add_Click({Write-Host $textBox.text})

$global:Arr[0] = $textBox.text
$OkButton.Add_Click({Write-Host $Arr})
$global:Arr[1] = "$textBox.text"
$OkButton.Add_Click({Write-Host $Arr})

Both elements get written as the value System.Windows.Forms.TextBox, Text: .text

I'm wondering how to make $textBox.text display in the Console in the array?

2
  • 1
    I'm not sure I quite understand but if you're saying that you are wondering why your $global:Arr[0] is blank, this is because you assign to it the value of $textBox.text, which is blank when that line is executed (That happen before the form appear). To set the first element value to the textbox, you'd do something like that instead: $OkButton.Add_Click( { $global:Arr[0] = $textBox.text }) Commented Dec 21, 2019 at 0:16
  • @SagePourpre Wow. it was that simple. My word! Thanks a ton for this. I spent days on this and would never have thought of that. Really,is there are source from which you'd recommend I'd read about PowerShell? I've primarily been using Udemy videos but most don't really explain everything I need in detail. BTW could you post this as the answer so I can Accept it. Thanks again. Commented Dec 21, 2019 at 2:25

1 Answer 1

2

The problem you face is simple.

You assumed that $global:Arr[1] = "$textBox.text" would be set the actual text value from when you click the Ok button in your form.

But actually, this section of code is called before $Form.ShowDialog(). Therefore, at that point in time, your textbox is indeed empty, which explains what you get.

If you want your global array to be set to the textbox text after the Ok button is pressed, you need to set the array value when the user click the button through the Add_Click event.

Like this:

 $OkButton.Add_Click( { $global:Arr[0] = $textBox.text })

Additional note

If you're not familiar already with how debugging work in Powershell, I'd recommend you to look at some debugging tutorials.

You would have immediately seen that the script did go to your variable assignment before the form is even shown, which would have possibly clued you in on the situation.

--

You can see below a debug point on the assignment line. The execution is stopped before I had a chance to see or enter anything in the form, which indicates that the value clearly can't be the one from the form. Querying $textBox.text at that point from the console shows the value to be empty, as we would expect from a newly initialized form. Debug exampel

Some documentation: how to debug scripts (even though it says ISE, all of this should be good with VSCode too)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the thorough explanation. Indeed i just started a few days ago trying to prepare a quick script working on a short timeline. I'll read into proper debugging right away. you're the best Sage. Thanks a ton man!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.