Search code examples
formspowershelltextbox

How to capture Textbox data using PowerShell and Windows Forms?


I am trying to create a windows form using PowerShell and then manipulate the data entered. The problem I am having is that I can get the code to work if I copy/paste it into a PowerShell window however I cannot get it to work if I save the exact same code to a PS1 file. I don't understand that.

If you try out the example in this article: http://technet.microsoft.com/en-us/library/ff730941.aspx it will work fine when you paste it into an open command prompt. If you try to save this code as a PS1 and run the PS1 in the PowerShell window I get nothing back when click OK on the dialog.

Can someone help me understand why it doesn't work as a PS1 file?


Solution

  • Variable assignment statement ($x=$objTextBox.Text) sets value within default scope, which is Local by default. As assignment statement is within {...} the variable value is not visible out of the assignment scope.

    You can change the assignment statement $x=$objTextBox.Text with the:

    $global:x=$objTextBox.Text
    

    More info: