Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell variable value is different

I’m new to Powershell/WPF and I’m having a simple problem that I couldn’t solve. I am trying to get the text from the variable = TextBox but I’m getting "System.Windows.Controls.Button: Connect" instead.

Here’s my PowerShell code.

$TextBox.Add_TextChanged({
$script:TextBoxPS = $TextBox.Text.ToString()
})

$Button.Add_Click({param($TextBoxPS)
Write-Host "$TextBoxPS"
})

I’m not sure what’s the problem. Can you guys please help? Thank you so much in advance!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In the button’s Click Event Handler, the parameter $TextBoxPS is the sender of the event (in this case, the button). You need to access the variable you assigned to in the textbox’s text changed event, which you assigned as a script scoped variable.

For example, change to:

$Button.Add_Click({param($TextBoxPS)
Write-Host "$($script:TextBoxPS)"
})

Read about Scopes here:
about_Scopes

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading