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.
  1. Function Get-FileLocImport ($initialDirectoryImport)
  2.  
  3. {
  4.  
  5.         [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
  6.  
  7.         $OpenFileDialogIn = New-Object System.Windows.Forms.OpenFileDialog
  8.  
  9.         $OpenFileDialogIn.initialDirectory = $initialDirectoryImport
  10.  
  11.         $OpenFileDialogIn.filter = "Excel (*.xlsx)| *.xlsx|Excel (*.xls)| *.xls|CSV - Comma Delimited (*.csv)| *.csv|All files (*.*)|*.*"
  12.  
  13.         $OpenFileDialogIn.ShowDialog() | Out-Null
  14.  
  15.         $OpenFileDialogIn.filename
  16.  
  17.         $OpenFileDialogIn.ShowHelp = $true
  18.  
  19.         $textbox1.Text = Get-Content $OpenFileDialogIn.filename
  20.  
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. $buttonIn_Click={
  30.  
  31.     #TODO: Place custom script here
  32.  
  33.     Get-FileLocImport
  34.  
  35.    
  36.  
  37. }

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 1508 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