Home > Net >  Powershell Form after rerunning form adds multiple return values
Powershell Form after rerunning form adds multiple return values

Time:12-15

I have been trying to create a Powershell form that uses the input to do other tasks. But the data needs to be validated (can't be blank, etc...) But when I run it and have to correct some of the inputs I get Multiple values from the return with the blank and my final. Also the variable doesn't leave the function. I have tried a number of things like Clear-Variable and Out-Null and I know it has to do with the way Powershell handles return data but I'm stuck. I have a simplified version here:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName Microsoft.VisualBasic

$BuildName = ""

function Enter-BuildInfo {
# Create a new form
$CoreForm = New-Object system.Windows.Forms.Form
$CoreForm.ClientSize = ‘220,100’
$CoreForm.text = “Enter Name”
$CoreForm.BackColor = “#03335a”
$Coreform.StartPosition = 'CenterScreen'
$Coreform.Topmost = $true
$OKButton = New-Object System.Windows.Forms.Button -Property @{
    Location = New-Object System.Drawing.Size(160,60)
    Size = New-Object System.Drawing.Size(50,25)
    BackColor = "#ffffff"
    Text = 'OK'
    DialogResult = [System.Windows.Forms.DialogResult]::OK
    }
$CoreForm.AcceptButton = $OKButton
$CoreForm.Controls.Add($OKButton)
### Inserting the text box that will accept input
$textbox1 = New-Object System.Windows.Forms.TextBox
$textbox1.Location = New-Object System.Drawing.Point(10,25) ### Location of the text box
$textbox1.Size = New-Object System.Drawing.Size(175,25) ### Size of the text box
$textbox1.Multiline = $false ### Allows multiple lines of data
$textbox1.AcceptsReturn = $false ### By hitting enter it creates a new line
#$textbox1.ScrollBars = "Vertical" ### Allows for a vertical scroll bar if the list of text is too big for the window
$Coreform.Controls.Add($textbox1)
$BuildName = $textbox1.text

$Coreresult = $CoreForm.ShowDialog()

if ($Coreresult -eq [Windows.Forms.DialogResult]::OK) 
    {
        $BuildName = $textbox1.text
    }   
    if ($BuildName -ne "")
    {
        Write-Host "Click OK Name: $BuildName "
    }
    else 
    {
        [Microsoft.VisualBasic.Interaction]::MsgBox("BuildName can not be blank",'OKOnly,SystemModal,Information', 'Retry Name')
        Enter-BuildInfo
    }
Write-Host "Inside Function Name: $BuildName "
}
Enter-BuildInfo

Write-Host "Out of Function Name: $BuildName "

My results:

Ok
Click OK Name: Test Name 
Inside Function Name: Test Name 
Inside Function Name:  
Out of Function Name:

CodePudding user response:

  • Variables defined in a function are never visible to the function's caller.

    • Make your function output data (do not use Write-Host for that) that you want the caller to see, which the caller has to capture ($output = Enter-BuildInfo)
  • Perform input validation in WinForm forms via event handlers that are called before the form closes (returns from the .ShowDialog() call).

    • That way there's no need for recursive calls to your function, which complicate matters.

    • Specifically:

      • Initialize your $OKButton's .Enabled property to $false
      • Pass an event handler (script block) to $textbox1.add_TextChanged(), in which you enable $OKButton only if $textbox1.Text.Trim() -ne ''

Note: To handle the enabling logic for the OK button based on multiple other controls:

  • Define a function that implements the validation logic based on the state of all relevant controls.

  • Call that function from the script blocks passed to the .add_{eventName}() methods of all relevant controls.

  • Related