Reputation: 47
I have a user interface that has a button and a text box. The button opens up OpenFiledDialog where a file is selected. I want to capture the path/filename from the System.Windows.Forms.OpenFileDialog and place it into a 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)
The 'browse' button calls the function Get-FileName. Please see my code.
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
It doesn't have to be a function that is called; I just want it to work. Currently it is not putting the selected file's path int he textbox.
Upvotes: 1
Views: 4066
Reputation: 2149
To change the text in a texbox you can use the .Text option like so:
$Button3.Add_Click({$InputBox3.Text = Get-FileName -initialDirectory "d:\pathname"})
Here we change the text of InputBox3 by calling the GetFileName function that way the text in InputBox3 will then be the result of Get-FileName (the filename the person selects)
Upvotes: 3