Home > OS >  PowerShell variable value is different
PowerShell variable value is different

Time:05-20

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!

CodePudding user response:

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

  • Related