【问题标题】:How to fill textbox with filename from OpenFileDialog?如何用 OpenFileDialog 中的文件名填充文本框?
【发布时间】:2014-05-05 15:34:26
【问题描述】:

我有一个带有按钮和文本框的用户界面。该按钮打开 OpenFiledDialog,其中选择了一个文件。我想从 System.Windows.Forms.OpenFileDialog 捕获路径/文件名并将其放入 System.Windows.Forms.TextBox。

###############Browse Button################################################################
$Button3 = New-Object System.Windows.Forms.Button
$Button3.Location = New-Object System.Drawing.Point(23,219)
$Button3.Size = New-Object System.Drawing.Size(23,23)
$Button3.Text = "..."
$Button3.Add_Click({Get-FileName -initialDirectory "d:\pathname"})
$Form1.Controls.Add($Button3)
###############SpreadSheetBox##################################################################
$InputBox3 = New-Object System.Windows.Forms.TextBox
$InputBox3.Location = New-Object System.Drawing.Size(51,219)
$InputBox3.Size = New-Object System.Drawing.Size(220,310)
$InputBox3.Multiline= $false
$Form1.Controls.Add($InputBox3)

“浏览”按钮调用函数 Get-FileName。请看我的代码。

Function Get-FileName($initialDirectory)
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null
 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.Title = "Choose a spreadsheet"
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "Excel Worksheet (*.xlsx)| *.xlsx"
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename

它不一定是被调用的函数;我只是想让它工作。目前它没有将所选文件的路径放入文本框中。

【问题讨论】:

    标签: winforms powershell


    【解决方案1】:

    要更改 texbox 中的文本,您可以使用 .Text 选项,如下所示:

    $Button3.Add_Click({$InputBox3.Text = Get-FileName -initialDirectory "d:\pathname"})

    这里我们通过调用 GetFileName 函数来更改 InputBox3 的文本,这样 InputBox3 中的文本将成为 Get-FileName(用户选择的文件名)的结果

    【讨论】:

      猜你喜欢
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      相关资源
      最近更新 更多