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()