Update the TextBox text field with the filename return from OpenDialog Filename in PowerShell

58 Views Asked by At

I used PowerShell to create a Windows Form with a TextBox field and a Button object. When I clicked the Button it will open the OpenFileDialog for user to select a file. How do I return the selected filename from OpenFileDialog and display in the TextBox field ?

Sample Code as follows:

$Filename = New-Object TextBox
$Filename.Location = New-Object System.Drawing.Point(230, 80)
$Filename.Size = New-Object System.Drawing.Size(400, 80)
$Filename.Font = New-Object System.Drawing.Font("Calibri", 12, [FontStyle]::Bold)
# $Filename.Text = $OpenFileDialog.FileName

$BrowseButton = New-Object Button
$BrowseButton.Location = New-Object System.Drawing.Point(650, 80)
$BrowseButton.Size = New-Object System.Drawing.Size(100, 30)
$BrowseButton.Font = New-Object System.Drawing.Font("Calibri", 12, [FontStyle]::Bold)
$BrowseButton.Text = "Browse..."
$BrowseButton.Add_Click($BrowseButton_Click)

$BrowseButton_Click =
{
  $OpenFileDialog = New-Object OpenFileDialog

  $OpenFileDialog.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*"
  $OpenFileDialog.FilterIndex = 2
  $OpenFileDialog.RestoreDirectory = $true

  if ($OpenFileDialog.ShowDialog() -eq [DialogResult]::OK)
  {
    # Do something to return the $OpenFileDialog.FileName to the TextBox field
  }
}
1

There are 1 best solutions below

2
Santiago Squarzon On BEST ANSWER

The line you have commented:

$Filename.Text = $OpenFileDialog.FileName

Should be what happens as a result of your if condition:

if ($OpenFileDialog.ShowDialog() -eq [DialogResult]::OK) {
    ....
}

In order to update the .Text property of your TextBox.

It's also worth noting that the $BrowseButton_Click ScriptBlock has to be defined before being added: $BrowseButton.Add_Click($BrowseButton_Click). PowerShell interprets code line-by-line and and as you have right now you will be trying to add $null to your .Click event.

In summary:

Add-Type -AssemblyName System.Windows.Forms

$form = [System.Windows.Forms.Form]@{
    Size = '1280, 720'
}

$Filename = New-Object System.Windows.Forms.TextBox
$Filename.Location = New-Object System.Drawing.Point(230, 80)
$Filename.Size = New-Object System.Drawing.Size(400, 80)
$Filename.Font = New-Object System.Drawing.Font('Calibri', 12, [System.Drawing.FontStyle]::Bold)
$form.Controls.Add($Filename)

$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.Filter = 'Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx|All files (*.*)|*.*'
$OpenFileDialog.FilterIndex = 2
$OpenFileDialog.RestoreDirectory = $true

$BrowseButton_Click = {
    if ($OpenFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
        $Filename.Text = $OpenFileDialog.FileName
    }
}

$BrowseButton = New-Object System.Windows.Forms.Button
$BrowseButton.Location = New-Object System.Drawing.Point(650, 80)
$BrowseButton.Size = New-Object System.Drawing.Size(100, 30)
$BrowseButton.Font = [System.Drawing.Font]::new('Calibri', 12, [System.Drawing.FontStyle]::Bold)
$BrowseButton.Text = 'Browse...'
$BrowseButton.Add_Click($BrowseButton_Click)
$form.Controls.Add($BrowseButton)
$form.ShowDialog()