Output browse to directory output as text in powershell GUI

Viewed 50

I'm working on a GUI that does some basic .csv editing, it utilizes a folderbrowserdialog prompt for the user to choose the directory in which the .csv editing takes place - currently this is all working, but I've found that trying to get the directory selected in the browser prompt isn't correctly being displayed on the GUI as a label. Instead the directory will only show when the script is run a second time - always showing the directory chosen from the previous run. How do I go about having a label that updates to display the chosen directory in the GUI? Currently cobbled together the following from previous searches:

$BrowseBtn.Add_Click({ Browse })
function Browse ($initialDirectory="")
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null

$foldername = New-Object System.Windows.Forms.FolderBrowserDialog
$foldername.Description = "Select a folder"
$foldername.rootfolder = "MyComputer"
$foldername.SelectedPath = $initialDirectory

if($foldername.ShowDialog() -eq "OK")
{
    $folder += $foldername.SelectedPath
}
return $folder
}
$path=Browse
$Directory = New-Object system.Windows.Forms.Label
$Directory.text=$path
$Directory.AutoSize = $false
$Directory.width = 350
$Directory.height = 50
$Directory.location = New-Object System.Drawing.Point(20,220)
$Directory.Font = 'Microsoft Sans Serif,10'

Any feedback on this would be greatly appreciated.

1 Answers

Example below.

Avoid using $path as there is a system/environment variable by this name

Add-Type -AssemblyName System.Windows.Forms
#
$form = New-Object System.Windows.Forms.Form
$form.Size = '830,495'
$form.StartPosition = 'CenterScreen'

$BrowseBtn = New-Object System.Windows.Forms.Button
$BrowseBtn.Location = '10,46'
$BrowseBtn.Size = '120,25'
$BrowseBtn.Text = "Browse"

$Directory = New-Object system.Windows.Forms.Label
$Directory.text='Path:'
$Directory.AutoSize = $false
$Directory.Size = '350,50'
$Directory.location = New-Object System.Drawing.Point(20,220)
$Directory.Font = 'Microsoft Sans Serif,10'

$form.Controls.AddRange(@($BrowseBtn,$Directory))
#
function Browse ($initialDirectory="") {
# You don't need to load the assembly again, it's loaded at the top of the form in `Add-Type`
    $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
    $foldername.Description = "Select a folder"
    $foldername.rootfolder = "MyComputer"
    $foldername.SelectedPath = $initialDirectory

    if($foldername.ShowDialog() -eq "OK") {
        $folder += $foldername.SelectedPath
    }
    return $folder
}


#
$BrowseBtn.Add_Click({ 
# Call function and store returned result in $Script:FolderPath.
# This will now be available generally to the script
# Without prefix ($FolderPath), it would only be available within the `Add_Click` section
# (Search 'Powershell Scoping' for details)
    $Script:FolderPath = Browse
# Change the label text. If you want to change the form it has to be as a result of some action like a button click etc
    $Directory.text= "Path: " + $FolderPath
 })

# Display form
$form.ShowDialog() | Out-Null
Related