Question on a Variable's Scope Relative To Click Event Handler Creation

I need someone with more experience then me, a relative newcomer on the subject of scope. I needed a form box and saw one here at this  Poweshell Link and the code says to do this:

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$Senior_Options_CSV = $objTextBox.Text;$Senior_Directory = $objTextBox1.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

Well for some time I could not get this form to work. Finally someone at StackOverflow pointed me in the right direction. There was a scope issue. The $x variable(Changed in my code) had a local scope relative to the OKButton object creation. I had to do a kludge of:

#Kludge workaround because there is some sort of scope problem with the script.
$Senior_Options_CSV = $objTextBox.Text
$Senior_Directory = $objTextBox1.Text

later down in the script to get the values input from the User assigned to $Senior_Options_CSV & $Senior_Directory variables so I could then use them in the script. I was hoping if someone can explain to me if this was an error in the post or how does one implement the click event handler so you can use variables in your program globally. Thanks...




  • Edited by AlanK87 8 hours 56 minutes ago Formating
January 31st, 2015 5:52pm

I have twice shown you the answer to this.  Why is it still an issue?

From what you have posted here it is not at all possible to understand your issue or to explain any resolutions.

Go back to the original discussion and address the original issue.  By just adding new variatins on the same question you are not likely to find an answer.

Sorry but you are just going in circles and frustrating yourself.

Free Windows Admin Tool Kit Click here and download it now
January 31st, 2015 7:20pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics