我已经找到了很多关于如何使用打开文件对话框为变量分配路径的文档,但是我已经能够找到如何在涉及Powershell中Add_Click事件的按钮的更复杂场景中执行此操作。
一些背景:我正在编写一个基本上将PDF转换为.txt的应用程序。
我的目标是让用户在按下“浏览”按钮后选择带有打开文件对话框的PDF。一旦他们抓住了pdf,我希望OK按钮接受路径作为变量,然后将选定的PDF转换为.txt文件。换句话说,我不希望他们自己进入这条道路;我希望浏览按钮提示打开文件对话框,然后,能够将所选路径用作变量。
这是完整的代码:
[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({$Dialog = $FileBrowser.ShowDialog(); if($Dialog -eq 'OK') {return $FileBrowser.SelectedPath}})
#
#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 $File })
$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
需要修改的代码涉及$ FileBrowser变量,$ BrowseButton变量和$ OkButton变量:
$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({$Dialog = $FileBrowser.ShowDialog(); if($Dialog -eq 'OK') {return $FileBrowser.SelectedPath}})
$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 $File })
$okButton.UseVisualStyleBackColor = $true
现在,我将满足于只使用带有Ok按钮或类似内容的Add_Click事件来回显路径。我怎样才能1)使用浏览按钮将所选文件存储在变量中(最好是添加点击事件),2)将该变量(是否需要全局工作?)传递给OK按钮的Add_Click函数
答案 0 :(得分:0)
在另一个论坛上收到了答案。
$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({
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
$FileBrowser.Filter = 'PDFs (*.pdf)|*.pdf'
if($FileBrowser.ShowDialog() -eq "Ok"){
$Script:filename = 'Not found'
$Script:filename = $TextBox.Text = $FileBrowser.Filename
}})
$TextBox.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @([System.Int32]100,[System.Int32]212))
$TextBox.Name = [System.String]'Path'
$TextBox.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @([System.Int32]75,[System.Int32]300))
如果我正确理解了这段代码,我的问题就是几件事。一个问题是范围界定。在add click函数中生成对象并添加$ script scope修饰符的事实解决了这个问题,并允许此处的函数在脚本的其他地方有用。第二个是告诉脚本什么时候做什么" ok"在文件浏览器中按下(或打开)。这是使用条件语句处理的。
最后,我处理运行pdftotext应用程序的方式出错了。我不需要将其作为add_click事件运行。我只是简单地将它添加到我的脚本底部,它实际上接受变量作为参数。
.\Includes\Bin32\pdftotext $filename
最终这适用于我的目的。