1

I'm trying to create a Windows form via Powershell and I need to capture the file path and store it in a variable. After the user clicks the 'Select' button and chooses the file, I would like to store the file path in a variable. Can someone please help me with this? The part of the code that shows the file path is the $selectButton.Add_Click() method.

    $folderForm = New-Object System.Windows.Forms.Form
    $pathTextBox = New-Object System.Windows.Forms.TextBox
    $fileBrowser = New-Object System.Windows.Forms.OpenFileDialog
    $selectButton = New-Object System.Windows.Forms.Button
    $okButton = New-Object System.Windows.Forms.Button
    $cancelButton = New-Object System.Windows.Forms.Button
    $WarningText = New-Object System.Windows.Forms.Label
    $FormTitleText = New-Object System.Windows.Forms.Label
    $PathLabel = New-Object System.Windows.Forms.Label
    $FormTitle = New-Object System.Windows.Forms.Label

    $folderForm.Width = 650
    $folderForm.Height = 410
    $folderForm.Text = 'Private IP Program'

    $FormTitle.Text = "Verizon Private IP Network Diagram"
    $FormTitle.Location = '175,10'

    $PathLabel.Text = 'Path:'
    $PathLabel.Location = '5,40'
    $pathTextBox.Size = '495,20'
    $pathTextBox.Location = '50,40'

    $selectButton.Location = '550,40'
    $WarningText.Location = '5, 100'

    $FormTitle.Text = "Verizon Private IP Network Diagram"
    $FormTitle.Location = '175,10'

    $WarningText.AutoSize = $true
    $FormTitle.AutoSize = $true

    $folderForm.Controls.Add($WarningText)
    $folderForm.Controls.Add($pathTextBox)
    $folderForm.Controls.Add($selectButton)
    $folderForm.Controls.Add($PathLabel)
    $folderForm.Controls.Add($FormTitle)

    $selectButton.Text = 'Select'
    $WarningText.Text = "Warning: Please note that manual arrangement of locations is needed after 30 sites."

    $selectButton.Add_Click({
        $fileBrowser.ShowDialog()
        $pathTextBox.Text = $fileBrowser.FileName
    })

    #$pathTextBox.ReadOnly = $true
    #$test.ReadOnly = $true

    $okButton.Text = "Ok"
    $okButton.Location = '225,200'

    $cancelButton.Text = "Cancel"
    $cancelButton.Location = "325,200"

    $folderForm.AcceptButton = $okButton
    $folderForm.CancelButton = $cancelButton

    $okButton.Add_Click({
        Main-Program
        $folderForm.Close()
    })

    $folderForm.Controls.Add($okButton)
    $folderForm.Controls.Add($cancelButton)

    $folderForm.ShowDialog()
mklement0
  • 382,024
  • 64
  • 607
  • 775

1 Answers1

1

Following your .ShowDialog() call, you can simply query the value of your $pathTextBox text-box object.

# ...

# Show the dialog modally and wait for it to close.
$null = $folderForm.ShowDialog()

# Get the selected path from the textbox and store it in a variable.
# You probably want to verify that the value is valid (not empty and
# refers to an existing file).
$selectedFilePath = $pathTextBox.Text

# Save it to a file.
$selectedFilePath > SelectedFilePath.txt

Note: if you do need to populate a variable before the dialog closes, use $script:selectedFilePath to define a variable in the script scope from inside your $selectButton.Add_Click() script block, which is necessary because event-handler script blocks run in child scopes - see this answer.


Here's a self-contained, adapted version of your code that illustrates the post-.ShowDialog() approach:

  • The Main-Program call was moved to after the .ShowDialog() call and after assignment of the $selectedFilePath variable.

  • The $okButton.Add_Click() event handler was removed in favor of $okButton.DialogResult = 'OK', which makes the button close the form automatically and makes .ShowDialog() return [System.Windows.Forms.DialogResult]::OK

  • The code also shows how to test if the dialog was closed via the OK button or not and ensures that the path isn't empty or blank.

Add-Type -AssemblyName System.Windows.Forms

$folderForm = New-Object System.Windows.Forms.Form
$pathTextBox = New-Object System.Windows.Forms.TextBox
$fileBrowser = New-Object System.Windows.Forms.OpenFileDialog
$selectButton = New-Object System.Windows.Forms.Button
$okButton = New-Object System.Windows.Forms.Button
$cancelButton = New-Object System.Windows.Forms.Button
$WarningText = New-Object System.Windows.Forms.Label
$FormTitleText = New-Object System.Windows.Forms.Label
$PathLabel = New-Object System.Windows.Forms.Label
$FormTitle = New-Object System.Windows.Forms.Label

$folderForm.Width = 650
$folderForm.Height = 410
$folderForm.Text = 'Private IP Program'

$FormTitle.Text = "Verizon Private IP Network Diagram"
$FormTitle.Location = '175,10'

$PathLabel.Text = 'Path:'
$PathLabel.Location = '5,40'
$pathTextBox.Size = '495,20'
$pathTextBox.Location = '50,40'

$selectButton.Location = '550,40'
$WarningText.Location = '5, 100'

$FormTitle.Text = "Verizon Private IP Network Diagram"
$FormTitle.Location = '175,10'

$WarningText.AutoSize = $true
$FormTitle.AutoSize = $true

$folderForm.Controls.Add($WarningText)
$folderForm.Controls.Add($pathTextBox)
$folderForm.Controls.Add($selectButton)
$folderForm.Controls.Add($PathLabel)
$folderForm.Controls.Add($FormTitle)

$selectButton.Text = 'Select'
$WarningText.Text = "Warning: Please note that manual arrangement of locations is needed after 30 sites."

$selectButton.Add_Click( {
        $fileBrowser.ShowDialog()
        $pathTextBox.Text = $fileBrowser.FileName
    })

$okButton.Text = "Ok"
$okButton.Location = '225,200'
# This ensures that the button closes the form and
# that .ShowDialog() reports [System.Windows.Forms.DialogResult]::OK
$okButton.DialogResult = 'OK'

$cancelButton.Text = "Cancel"
$cancelButton.Location = "325,200"

$folderForm.AcceptButton = $okButton
$folderForm.CancelButton = $cancelButton

$folderForm.Controls.Add($okButton)
$folderForm.Controls.Add($cancelButton)

if ($folderForm.ShowDialog() -eq 'OK' -and $pathTextBox.Text.Trim()) {
    $selectedFilePath = $pathTextBox.Text.Trim()
    $selectedFilePath > SelectedPath.txt
    Write-Verbose -vb "Proceeding with path $selectedFilePath..."
    # ... call code that relies on $selectedFilePath or to which pass it as an argument.
    Main-Program
}
else {
    Write-Warning 'Canceled or no path specified.'
}
mklement0
  • 382,024
  • 64
  • 607
  • 775