none
How Do You Assign a Path to a Variable via a Browse Button (Open File Dialog) and Click Event?

    Question

  • I've found a lot of documentation for how to assign a path to a variable with the open file dialog, but none of them address this for a more complex scenario involving a browse button. Usually the sample scripts I've found automatically open the file browser dialog, and do not involve a button. 

    Some background: I'm writing an application that basically converts PDFs to .txt.

    My goal is for the user to choose the PDF with an open file dialog after pressing a "browse" button, which initializes the open file dialog with an Add_Click event. Once they've grabbed the pdf, I want the OK button to accept the path as a variable and, voila, convert the selected PDF into a .txt file.

    In other words, I don't want them entering the path on their own; I want the browse button to prompt the open file dialog, and thereafter (once they've clicked OK), have the ability to use the selected path as a variable. 

    My question then is in two parts: 1) How do I store the path as a variable once the OK button has been when a user selects a pdf from the file browser? 2) How do I pass the variable to a separate button with an Add_Click event?

    Friday, June 08, 2018 6:39 PM

Answers

  • I don't know where you found that code but it is not very good and shows little understanding of forms and PowerShell.

    Here is an example that exactly answers your original question.

    add-type -AssemblyName System.Windows.Forms
    
    $form = New-Object System.Windows.Forms.Form
    $form.StartPosition = 'CenterScreen'
    
    $button = New-Object System.Windows.Forms.Button
    $form.Controls.Add($button)
    $button.Text = 'Get file'
    $button.Location = '10,10'
    $button.add_Click({
        $ofd = New-Object system.windows.forms.Openfiledialog
        $ofd.Filter =  'PDFs (*.pdf)|*.pdf' 
        $script:filename = 'Not found'
        if($ofd.ShowDialog() -eq 'Ok'){
            $script:filename = $textbox.Text = $ofd.FileName
        }    
    })
    
    $buttonOK = New-Object System.Windows.Forms.Button
    $form.Controls.Add($buttonOK)
    $buttonOK.Text = 'Ok'
    $buttonOK.Location = '10,40'
    $buttonOK.DialogResult = 'OK'
    
    $textbox = New-Object System.Windows.Forms.TextBox
    $form.Controls.Add($textbox)
    $textbox.Location = '100,10'
    $textbox.Width += 50
    
    $form.ShowDialog()
    $filename
    

    Forms are very difficult for non-programmers.  Start simple and build one piece at a time.


    \_(ツ)_/

    • Marked as answer by aeakins Thursday, June 14, 2018 7:01 PM
    Wednesday, June 13, 2018 8:01 PM
    Moderator

All replies

  • add-type -AssemblyName System.Windows.Forms
    $ofd =[system.windows.forms.Openfiledialog]::New()
    if($ofd.ShowDialog() -eq 'Ok'){
        $script:filename = $ofd.FileName
    }

    The scope specifier will make the name visible in the button click event.  You can also just store the name in a textbox which is visible by default.

    Customization instructions are here: https://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396


    \_(ツ)_/



    Friday, June 08, 2018 7:44 PM
    Moderator
  • Gave this a shot but wasn't able to get it working. 

    [void][System.Reflection.Assembly]::Load('System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    [void][System.Reflection.Assembly]::Load('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    $MainForm = New-Object -TypeName System.Windows.Forms.Form
    [System.Windows.Forms.Button]$okButton = $null
    [System.Windows.Forms.Button]$cancelButton = $null
    [System.Windows.Forms.PictureBox]$pictureBox1 = $null
    [System.Windows.Forms.Label]$Label = $null
    [System.Windows.Forms.OpenFileDialog]$openFileDialog1 = $null
    [System.Windows.Forms.Button]$BrowseButton = $null
    [System.Windows.Forms.Button]$button1 = $null
    function InitializeComponent
    {
    #$resources = Invoke-Expression (Get-Content "C:\Users\eakinsa\Desktop\Style Guide Report\Includes\Form1.resources.psd1" -Raw)
    $okButton = (New-Object -TypeName System.Windows.Forms.Button)
    $cancelButton = (New-Object -TypeName System.Windows.Forms.Button)
    $pictureBox1 = (New-Object -TypeName System.Windows.Forms.PictureBox)
    $Label = (New-Object -TypeName System.Windows.Forms.Label)
    $openFileDialog1 = (New-Object -TypeName System.Windows.Forms.OpenFileDialog)
    $BrowseButton = (New-Object -TypeName System.Windows.Forms.Button)
    ([System.ComponentModel.ISupportInitialize]$pictureBox1).BeginInit()
    $MainForm.SuspendLayout()

    #
    #BrowseDialog Button
    #


    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
        Multiselect = $false # Multiple files can be chosen
    Filter = 'PDFs (*.pdf)|*.pdf' # Specified file types
    }
     

    $BrowseButton.FlatStyle = [System.Windows.Forms.FlatStyle]::Popup
    $BrowseButton.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]16,[System.Int32]212))
    $BrowseButton.Name = [System.String]'BrowseButton'
    $BrowseButton.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]75,[System.Int32]23))
    $BrowseButton.TabIndex = [System.Int32]1
    $BrowseButton.Text = [System.String]'Browse'
    $BrowseButton.UseVisualStyleBackColor = $true
    $BrowseButton.Add_Click({if($FileBrowser.ShowDialog() -eq 'Open'){
        $Script:filename = $FileBrowser.FileName
    }})



    #
    #cancelButton-Cancel
    #
    $cancelButton.ForeColor = [System.Drawing.Color]::Crimson
    $cancelButton.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]545,[System.Int32]439))
    $cancelButton.Name = [System.String]'cancelButton'
    $cancelButton.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]75,[System.Int32]23))
    $cancelButton.TabIndex = [System.Int32]2
    $cancelButton.Text = [System.String]'Cancel'
    $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $cancelButton.UseVisualStyleBackColor = $true

    #
    #okButton-Okay
    #
    $okButton.ForeColor = [System.Drawing.Color]::ForestGreen
    $okButton.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]626,[System.Int32]440))
    $okButton.Name = [System.String]'okButton'
    $okButton.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]75,[System.Int32]23))
    $okButton.TabIndex = [System.Int32]3
    $okButton.Text = [System.String]'OK'
    $okButton.Add_Click({echo $FileBrowser.FileName; })
    $okButton.UseVisualStyleBackColor = $true

    #
    #pictureBox1
    #

    #$pictureBox1.Image = ([System.Drawing.Image]$resources.'pictureBox1.Image')
    #$pictureBox1.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]12,[System.Int32]12))
    #$pictureBox1.Name = [System.String]'pictureBox1'
    #$pictureBox1.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]689,[System.Int32]165))
    #$pictureBox1.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::Zoom
    #$pictureBox1.TabIndex = [System.Int32]2
    #$pictureBox1.TabStop = $false
    #$pictureBox1.add_Click($pictureBox1_Click)
    #
    #Label
    #
    $Label.AutoSize = $true
    $Label.ForeColor = [System.Drawing.Color]::Black
    $Label.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]13,[System.Int32]184))
    $Label.Name = [System.String]'Label'
    $Label.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]570,[System.Int32]13))
    $Label.TabIndex = [System.Int32]3
    $Label.Text = [System.String]'Use this tool to get a custom report that flags potential style errors in a PDF based on the Elsevier style guide.'
    #

    #MainForm
    #
    $MainForm.ClientSize = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]713,[System.Int32]475))
    $MainForm.Controls.Add($BrowseButton)
    $MainForm.Controls.Add($Label)
    $MainForm.Controls.Add($pictureBox1)
    $MainForm.Controls.Add($cancelButton)
    $MainForm.Controls.Add($okButton)
    $MainForm.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList @([System.String]'Segoe UI',[System.Single]8.25,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))
    $MainForm.ForeColor = [System.Drawing.Color]::ForestGreen
    $MainForm.Name = [System.String]'MainForm'
    $MainForm.Text = [System.String]'PDF Style Reporting Tool'
    ([System.ComponentModel.ISupportInitialize]$pictureBox1).EndInit()
    $MainForm.ResumeLayout($false)
    $MainForm.PerformLayout()
    $MainForm.ShowDialog()
    Add-Member -InputObject $MainForm -Name base -Value $base -MemberType NoteProperty
    Add-Member -InputObject $MainForm -Name okButton -Value $okButton -MemberType NoteProperty
    Add-Member -InputObject $MainForm -Name pictureBox1 -Value $pictureBox1 -MemberType NoteProperty
    Add-Member -InputObject $MainForm -Name Label -Value $Label -MemberType NoteProperty
    Add-Member -InputObject $MainForm -Name openFileDialog1 -Value $openFileDialog1 -MemberType NoteProperty
    Add-Member -InputObject $MainForm -Name BrowseButton -Value $BrowseButton -MemberType NoteProperty
    Add-Member -InputObject $MainForm -Name button1 -Value $button1 -MemberType NoteProperty
    }
    . InitializeComponent
    Wednesday, June 13, 2018 7:38 PM
  • I don't know where you found that code but it is not very good and shows little understanding of forms and PowerShell.

    Here is an example that exactly answers your original question.

    add-type -AssemblyName System.Windows.Forms
    
    $form = New-Object System.Windows.Forms.Form
    $form.StartPosition = 'CenterScreen'
    
    $button = New-Object System.Windows.Forms.Button
    $form.Controls.Add($button)
    $button.Text = 'Get file'
    $button.Location = '10,10'
    $button.add_Click({
        $ofd = New-Object system.windows.forms.Openfiledialog
        $ofd.Filter =  'PDFs (*.pdf)|*.pdf' 
        $script:filename = 'Not found'
        if($ofd.ShowDialog() -eq 'Ok'){
            $script:filename = $textbox.Text = $ofd.FileName
        }    
    })
    
    $buttonOK = New-Object System.Windows.Forms.Button
    $form.Controls.Add($buttonOK)
    $buttonOK.Text = 'Ok'
    $buttonOK.Location = '10,40'
    $buttonOK.DialogResult = 'OK'
    
    $textbox = New-Object System.Windows.Forms.TextBox
    $form.Controls.Add($textbox)
    $textbox.Location = '100,10'
    $textbox.Width += 50
    
    $form.ShowDialog()
    $filename
    

    Forms are very difficult for non-programmers.  Start simple and build one piece at a time.


    \_(ツ)_/

    • Marked as answer by aeakins Thursday, June 14, 2018 7:01 PM
    Wednesday, June 13, 2018 8:01 PM
    Moderator
  • Thanks JRV. This helps resolve my issue more than anything else. 

    I am indeed new to PowerShell forms, and, yeah, my trade isn't programming - just something I tinker with once in awhile for task automation. Though I'm curious: what makes you say the code is bad?

    To be certain, I didn't code it from scratch. The first PowerShell form I did as much with was highly frustrating in terms of design, so I used some software in Visual Studio (Powershell Pro Tools) to get the basic layout done quickly. That introduced a lot of extraneous code that I definitely plan to refine more - so any tips in that regard would be welcome.  



    • Edited by aeakins Thursday, June 14, 2018 6:55 PM
    Thursday, June 14, 2018 6:53 PM
  • All VS tools assume you are a programmer.  Without sufficient knowledge of FOrms and PwoerShell this will be a challenge.

    Start by becoming proficient I n PowerShell then look up examples of forms and learn how they are intended to work.

    These articles will help if you are proficient in PowerSHell.

    https://info.sapien.com/index.php/guis

    Be sure to select the other article categories on the left menu.


    \_(ツ)_/

    Thursday, June 14, 2018 6:58 PM
    Moderator