Page 1 of 1

Adding File Location into a Text Box

Posted: Tue Feb 05, 2019 3:41 pm
by Skeletor2012
Hi,

I am new to PowerShell, and very new to PowerShell Studio.

I have a script that involves specifying a filename, and adding this filename and path into a textbox.
I am struggling to get the correct data to be show in the textbox, and calling on the variable in the console shows as Null.

Code: Select all

Function Get-FileLocImport ($initialDirectoryImport)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$OpenFileDialogIn = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialogIn.initialDirectory = $initialDirectoryImport
$OpenFileDialogIn.filter = "Excel (*.xlsx)| *.xlsx|Excel (*.xls)| *.xls|CSV - Comma Delimited (*.csv)| *.csv|All files (*.*)|*.*"
$OpenFileDialogIn.ShowDialog() | Out-Null
$OpenFileDialogIn.filename
$OpenFileDialogIn.ShowHelp = $true
$textbox1.Text = Get-Content $OpenFileDialogIn.filename
}



$buttonIn_Click={
#TODO: Place custom script here
Get-FileLocImport

}

Can any one help please?

Thanks.

Re: Adding File Location into a Text Box

Posted: Tue Feb 05, 2019 4:26 pm
by jvierra
First you do not need to laod the assembly in forms code. It is already loaded.

You also don't need to create a function. Just place the code in the event that you want to use to get the file name.

Code: Select all

$buttonIn_Click = {
    $OpenFileDialogIn.initialDirectory = $initialDirectoryImport
    $OpenFileDialogIn.filter = "Excel (*.xlsx)| *.xlsx|Excel (*.xls)| *.xls|CSV - Comma Delimited (*.csv)| *.csv|All files (*.*)|*.*"
    $OpenFileDialogIn.ShowHelp = $true
    $OpenFileDialogIn.ShowDialog() | Out-Null
    $textbox1.Text = $OpenFileDialogIn.filename
}
Just add the OpenFileDialog control to the form in the designer.

Before trying to build forms review the following articles and documents: https://info.sapien.com/index.php/guis

Re: Adding File Location into a Text Box

Posted: Wed Feb 06, 2019 8:32 am
by davidc
You can use the TextBox - Browse for File control set for this:
Control Set - TextBox Browse For File.png
Control Set - TextBox Browse For File.png (28.4 KiB) Viewed 695 times
Then you just need to update the OpenFileDialog's properties in the designer.

Re: Adding File Location into a Text Box

Posted: Fri Feb 08, 2019 3:33 pm
by Skeletor2012
Thanks for your posts.
Way easier to use than trying to script from scratch.

Re: Adding File Location into a Text Box

Posted: Thu Feb 14, 2019 10:22 am
by lyolls
Thanks for the answers. It useful to me, as I also a newbie to PowerShell and I want to improve my knowledge in this direction. So, in this regard I want to know maybe you can advise me some tutorials that can be on sapien, because I'm doing a project here and as I junior system admin I want to understand how to use command line faster for my needs.

Re: Adding File Location into a Text Box

Posted: Thu Feb 14, 2019 1:35 pm
by jvierra