powershell show selected file in OpenFileDialog

79 Views Asked by At

probably a simple question, but I dont get it now. I have to create a small dialog for merging two pdf-files.

I made the form with two file-choosing lines. At click in the line the file-choosing dialog opens up and its possible to choose a file. But - after choosing I cant see the filepath.

Is it possible to enter the value of the variable $pdffile_1 in the top line? How?

(I know - this is not ready and working now - its wip ;) )

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

function Select-File ($InitialDirectory) {
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.Title = "Bitte PDF auswählen"
    # $OpenFileDialog.InitialDirectory = $InitialDirectory
    $OpenFileDialog.filter = "All files (*.pdf)| *.pdf"
    If ($OpenFileDialog.ShowDialog() -eq "Cancel") {
        [System.Windows.Forms.MessageBox]::Show("Keine Datei ausgewählt!", "Error", 0, 
        [System.Windows.Forms.MessageBoxIcon]::Exclamation)
        $result = $null
    } 
    else { 
        $result = $OpenFileDialog.FileName
    }
    $OpenFileDialog.Dispose()

    return $result
}

#Basis-Fenster
$form = New-Object System.Windows.Forms.Form
$form.Topmost = $true
$form.StartPosition = 'CenterScreen'
$form.Text = 'PDF-Zusammenführungstool'
$form.Size = New-Object System.Drawing.Size(300,300)
# $Form.AutoSize = $True
# $Form.AutoSizeMode = "GrowAndShrink"

#Feintuning
#Powershell-Icon
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$Form.Icon = $Icon
# Hintergrundfarbe
$Form.Backcolor="white"


#Schaltflächen
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,75)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,75)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

#Infotext
# $label = New-Object System.Windows.Forms.Label
# $label.Location = New-Object System.Drawing.Point(10,20)
# $label.Size = New-Object System.Drawing.Size(280,20)
# $label.Text = 'PDF-Zusammenführungtool'
# $form.Controls.Add($label)

#Eingabefeld 1
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,40)
$textBox1.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox1)
$textBox1.Add_Click({ $Sel.Text = Select-File })
$pdffile_1=($sel).Text

Write-Host PDFFILE_1 is $pdffile_1

#Eingabefeld 2
$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(10,80)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)
$textBox2.Add_Click({ $Sel.Text = Select-File })

#Focus in textbox1
$form.Add_Shown({$textBox1.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    echo 'OK gedrückt'
    $x = $textBox.Text
    $x
}```

Thanks!
0

There are 0 best solutions below