Page 1 of 1

Adding File Location into a Text Box

Posted: Tue Feb 05, 2019 2: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.
  1. Function Get-FileLocImport ($initialDirectoryImport)
  2. {
  3.         [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
  4.         $OpenFileDialogIn = New-Object System.Windows.Forms.OpenFileDialog
  5.         $OpenFileDialogIn.initialDirectory = $initialDirectoryImport
  6.         $OpenFileDialogIn.filter = "Excel (*.xlsx)| *.xlsx|Excel (*.xls)| *.xls|CSV - Comma Delimited (*.csv)| *.csv|All files (*.*)|*.*"
  7.         $OpenFileDialogIn.ShowDialog() | Out-Null
  8.         $OpenFileDialogIn.filename
  9.         $OpenFileDialogIn.ShowHelp = $true
  10.         $textbox1.Text = Get-Content $OpenFileDialogIn.filename
  11. }
  12.  
  13.  
  14.  
  15. $buttonIn_Click={
  16.     #TODO: Place custom script here
  17.     Get-FileLocImport
  18.    
  19. }

Can any one help please?

Thanks.

Re: Adding File Location into a Text Box

Posted: Tue Feb 05, 2019 3: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 7: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 4018 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 2: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 9: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 12:35 pm
by jvierra