refactor: Replace Out-GridView with Form and ListBox in Get-FolderBrowser

This commit is contained in:
Bobby Abellana (aider) 2025-02-21 09:30:59 -08:00
parent 37ec5d328e
commit c30d7ccdc5

View File

@ -195,7 +195,7 @@ function Get-FolderBrowser {
if ($ParentFolderUrl) {
# Get the folder ID first
# Construct the correct URI for the folder
# Remove the /sites/tax/Shared Documents part from the folder path
# Remove the /sites/tax/Shared Documents/ part from the folder path
$relativePath = $ParentFolderUrl.Replace("/sites/tax/Shared Documents/", "").TrimStart("/")
# URL encode the relative path
@ -236,8 +236,49 @@ function Get-FolderBrowser {
}) + $folderObjects
}
# Show the Out-GridView
$selected = $folderObjects | Out-GridView -Title "Select Folder" -PassThru
# Create a form and listbox for folder selection
$form = New-Object System.Windows.Forms.Form
$form.Text = "Select Folder"
$form.Size = New-Object System.Drawing.Size(400, 400)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Size = New-Object System.Drawing.Size(360, 320)
$listBox.Location = New-Object System.Drawing.Point(10, 10)
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(150, 340)
$okButton.Size = New-Object System.Drawing.Size(75, 23)
$okButton.Text = "OK"
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(230, 340)
$cancelButton.Size = New-Object System.Drawing.Size(75, 23)
$cancelButton.Text = "Cancel"
$form.Controls.Add($listBox) | Out-Null
$form.Controls.Add($okButton) | Out-Null
$form.Controls.Add($cancelButton) | Out-Null
$form.FormBorderStyle = 'FixedDialog'
$form.StartPosition = 'CenterScreen'
$form.Add_Shown({$Form.Activate()})
# Populate listbox
foreach ($folder in $folderObjects) {
$listBox.Items.Add($folder.Name) | Out-Null
}
# Handle OK button click
$okButton.Add_Click({
$form.Tag = $listBox.SelectedItem
$form.Close()
})
# Handle Cancel button click
$cancelButton.Add_Click({
$form.Tag = $null
$form.Close()
})
# Show the Form as a Dialog
[void]$form.ShowDialog()
# Get the selected folder
$selected = $folderObjects | Where-Object {$_.Name -eq $form.Tag}
# If a folder is selected, browse into it
if ($selected -and $selected.IsFolder -eq $true -and $selected.Name -ne "..") {
@ -246,9 +287,12 @@ function Get-FolderBrowser {
elseif ($selected -and $selected.Name -eq "..") {
return Get-FolderBrowser -ParentFolderUrl $selected.ServerRelativeUrl
}
else {
elseif ($selected) {
return $selected
}
else {
return $null
}
}
catch {
$script:txtStatus.Text += "Folder Error: $($_.Exception.Message)`n"