0

`In a Powershell project - small GUI with Winforms - I need a passwordbox. I wrote a test-function but cannot manage to get the correct output ... I only get "Cancel" I get the same result if I do not use

$inpBox.PasswordChar      = "*"
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[System.Windows.Forms.Application]::EnableVisualStyles()

function InputForm {

    param($loggedInUser="Spock")
  
    $inpForm                  = New-Object System.Windows.Forms.Form
    $inpForm.Height           = 120
    $inpForm.Width            = 350
    # $inpForm.Icon             = "$PSScriptRoot\Heidelberg-H.ico"
    $inpForm.Text             = "Authentication Window"
    $inpForm.MaximizeBox      = $false
    $inpForm.MinimizeBox      = $false
    $inpForm.FormBorderStyle  = "FixedDialog"
    $inpForm.StartPosition    = "CenterScreen"  # moves to center of screen
    $inpForm.Topmost          = $true           # Make it Topmost
  
    # create a Label
    $inpLbl                   = New-Object System.Windows.Forms.Label
    $inpLbl.Width             = 300
    $inpLbl.Location          = New-Object System.Drawing.Point(10,10)
    $inpLbl.Text              = "Please type in the password for user: " + $loggedInUser
  
    # create a InputBox for the password
    $inpBox                   = New-Object System.Windows.Forms.TextBox
    $inpBox.Width             = 200
    $inpBox.Height            = 25
    $inpBox.PasswordChar      = "*"
    $inpBox.Text              = "********"
    $inpBox.Location          = New-Object System.Drawing.Point(10, 35)     
    
    # create an OK Button
    $inpBtn                   = New-Object System.Windows.Forms.Button
    $inpBtn.Width             = 60
    $inpBtn.Height            = 25
    $inpBtn.Text              = "OK"
    $inpBtn.Location          = New-Object System.Drawing.Point(250,33)
    $form.AcceptButton        = $inpBtn
  
    $inpForm.Controls.AddRange(@($inpLbl, $inpBox, $inpBtn))
  
    # OK-Button - Click event
    $inpBtn.Add_Click({

        $inpForm.Close()
        return $inpBox.Text

    })  
   
    $inpForm.ShowDialog()
}

# MAIN

$PW = InputForm (Get-WMIObject -ClassName Win32_ComputerSystem).Username

write-host $PW

1 Answer 1

0

The problem is with this last part of your function:

$inpBtn.Add_Click({

    $inpForm.Close()
    return $inpBox.Text

})  

$inpForm.ShowDialog()

Instead of registering a custom handler for the Click event, you'll want to assign a value to the DialogResult property on the button - then inspect that result and return the value of the textbox from the function:

$inpBtn.DialogResult = 'OK' 

if($inpForm.ShowDialog() -eq 'OK'){
    return $inpBox.Text
}
else {
    throw "User canceled password prompt!"
}
Sign up to request clarification or add additional context in comments.

3 Comments

Works perfect - thank you! But to be honest, I didn't understand why by now. Is there an Article I could read? Especially I do not understand the concept of $inpBtn.DialogResult = 'OK' ...
@Bernd When you assign a DialogResult value to a button, clicking that button automatically causes the parent form to close and your call to ShowDialog() will then return said DialogResult value - which is what we then use to determine whether the button was actually clicked, or whether something else closed it (eg. closing the form window).
Ok - found Button.DialogResult Property - very useful - thanks again.

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.