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
Joined: Tue Feb 05, 2019 9:58 pm

Adding File Location into a Text Box

Post by Skeletor2012 » Tue Feb 05, 2019 3:41 pm

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.

User avatar
jvierra
Posts: 13677
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Adding File Location into a Text Box

Post by jvierra » Tue Feb 05, 2019 4:26 pm

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
Joined: Thu Aug 18, 2011 4:56 am

Re: Adding File Location into a Text Box

Post by davidc » Wed Feb 06, 2019 8:32 am

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

Skeletor2012
Posts: 2
Joined: Tue Feb 05, 2019 9:58 pm

Re: Adding File Location into a Text Box

Post by Skeletor2012 » Fri Feb 08, 2019 3:33 pm

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

lyolls
Posts: 1
Joined: Thu Feb 14, 2019 6:09 pm

Re: Adding File Location into a Text Box

Post by lyolls » Thu Feb 14, 2019 10:22 am

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.

User avatar
jvierra
Posts: 13677
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Adding File Location into a Text Box

Post by jvierra » Thu Feb 14, 2019 1:35 pm


Locked