2

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:
2
  • 1
    In addition to mklement0's nice answer, this might be a matter of personal preference but ask yourself if using a function in this case is really needed or is there to make things more complicated than they should be. Usually, WinFrom apps on PS have the UI code outside of a function and the event's code as scriptblocks. Commented Dec 13, 2021 at 23:20
  • You are correct but I am a relatively new programmer and this is a sub-form to another much larger form. So I have already overcomplicated it. Commented Dec 14, 2021 at 14:05

1 Answer 1

1
  • 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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Eubs, please see my update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.