Adding File Location into a Text Box

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
Skeletor2012
Posts: 2
Last visit: Sun Feb 10, 2019 2:46 am

Adding File Location into a Text Box

Post 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.

jvierra
Posts: 14876
Last visit: Fri Jul 30, 2021 8:16 am
Answers: 12
Has voted: 3 times
Been upvoted: 10 times

Re: Adding File Location into a Text Box

Post 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

User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 1 time

Re: Adding File Location into a Text Box

Post 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 2873 times
Then you just need to update the OpenFileDialog's properties in the designer.
David
SAPIEN Technologies, Inc.

Skeletor2012
Posts: 2
Last visit: Sun Feb 10, 2019 2:46 am

Re: Adding File Location into a Text Box

Post by Skeletor2012 »

Thanks for your posts.
Way easier to use than trying to script from scratch.

lyolls
Posts: 1
Last visit: Thu Feb 14, 2019 9:23 am

Re: Adding File Location into a Text Box

Post 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.

jvierra
Posts: 14876
Last visit: Fri Jul 30, 2021 8:16 am
Answers: 12
Has voted: 3 times
Been upvoted: 10 times

Re: Adding File Location into a Text Box

Post by jvierra »


Locked