Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - add browse button to windows form

Reply
 
Old 01-18-2009   #1 (permalink)


Vista Home Premium 32bit
 
 

add browse button to windows form

Hi guys. It's the first time that I try to use windows forms in powershell. I'd like to add a browse button to choose a directory but obviously my code doesn't work.
Someone can put me in the right way?

[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$form = new-object Windows.Forms.Form
$browse = new-object windows.Forms.OpenFileDialog
$browse.text = "..."
$form.controls.add($browse)
$form.ShowDialog()

My System SpecsSystem Spec
Old 01-18-2009   #2 (permalink)
BJ


 
 

Re: add browse button to windows form

Hi sardinian_guy,
you cannot ad the OpenFileDialog to the Form as a control.
Add a Button and asign the OpenFileDialog to the KlickEvent of the
Button:

[void][reflection.assembly]::LoadWithPartialName
("System.Windows.Forms")
$form = new-object Windows.Forms.Form
$browse = new-object windows.Forms.OpenFileDialog
$browse.text = "..."

$button1 = New-Object system.Windows.Forms.Button
$button1.Text = "Choose...."
$button1.Add_Click({$browse.ShowDialog()})
$button1.left = 20
$button1.top = 20

$form.controls.add($button1)
$form.ShowDialog()


If you want to choose a Folder you should use the FolderBrowserDialog.
However i had problems with this in a Poweshell V2 CTP3 Environment -
it only runs in the ISE.

Example:

$button = $browse = $form = 0
[void][reflection.assembly]::LoadWithPartialName
("System.Windows.Forms")
$browse = new-object system.windows.Forms.FolderBrowserDialog
$browse.RootFolder = [System.Environment+SpecialFolder]'MyComputer'
$browse.ShowNewFolderButton = $false
$browse.selectedPath = "C:\"
$browse.Description = "Choose a directory"

$button1 = New-Object system.Windows.Forms.Button
$button1.Text = "Choose Directory"
$button1.Add_Click({$browse.ShowDialog()})
$button1.left = 20
$button1.top = 20

$form = New-Object system.windows.forms.Form
$form.controls.add($button1)
$form.ShowDialog()
$form.Dispose()

$browse.SelectedPath


Finally i do not recommend to use to much of this graphical stuff
with Powershell because it could be very extensive to arrange the
controls etc...
Using the Dialogs without the Form ist much easier.
My System SpecsSystem Spec
Old 01-18-2009   #3 (permalink)
bluefin


 
 

Re: add browse button to windows form

PowerShell is meant for console scripting rather than GUI, however,
you can use PrimalForms, a free GUI builder tool. It edits and stores
Windows Forms in a native XML format and generates PowerShell code on
demand. In addition, you must provide your own code e.g. function to
trigger the windows controls. Give it a try, http://www.primaltools.com/freetools/

Cheers.


On Jan 18, 7:18*pm, sardinian_guy <gu...@xxxxxx-email.com> wrote:
Quote:

> Hi guys. It's the first time that I try to use windows forms in
> powershell. I'd like to add a browse button to choose a directory but
> obviously my code doesn't work.
> Someone can put me in the right way?
>
> [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
> $form = new-object Windows.Forms.Form
> $browse = new-object windows.Forms.OpenFileDialog
> $browse.text = "..."
> $form.controls.add($browse)
> $form.ShowDialog()
>
> --
> sardinian_guy
My System SpecsSystem Spec
Old 01-19-2009   #4 (permalink)
tojo2000


 
 

Re: add browse button to windows form

On Jan 18, 3:10*pm, bluefin <desertcamel.c...@xxxxxx> wrote:
Quote:

> PowerShell is meant for console scripting rather than GUI, however,
> you can use PrimalForms, a free GUI builder tool. It edits and stores
> Windows Forms in a native XML format and generates PowerShell code on
> demand. In addition, you must provide your own code e.g. function to
> trigger the windows controls. Give it a try,http://www.primaltools.com/freetools/
>
> Cheers.
>
> On Jan 18, 7:18*pm, sardinian_guy <gu...@xxxxxx-email.com> wrote:
>
>
>
Quote:

> > Hi guys. It's the first time that I try to use windows forms in
> > powershell. I'd like to add a browse button to choose a directory but
> > obviously my code doesn't work.
> > Someone can put me in the right way?
>
Quote:

> > [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
> > $form = new-object Windows.Forms.Form
> > $browse = new-object windows.Forms.OpenFileDialog
> > $browse.text = "..."
> > $form.controls.add($browse)
> > $form.ShowDialog()
>
Quote:

> > --
> > sardinian_guy
Not necessarily. PowerShell is for automation. The majority of
examples that people have are for the console, but PowerShell was made
with more than this kind of automation in mind. That being said, I'd
second the suggestion to try PrimalForms. You can write forms by hand
as well, but that tool will make it much easier for you.
My System SpecsSystem Spec
Old 01-19-2009   #5 (permalink)
BJ


 
 

Re: add browse button to windows form

Good Point. didn't hear about that.
I think this can be usefull for little "Powershell Applications"....

Thanks for the Hint.
Best Regards
BJ

bluefin schrieb:
Quote:

> PowerShell is meant for console scripting rather than GUI, however,
> you can use PrimalForms, a free GUI builder tool. It edits and stores
> Windows Forms in a native XML format and generates PowerShell code on
> demand. In addition, you must provide your own code e.g. function to
> trigger the windows controls. Give it a try, http://www.primaltools.com/freetools/
>
> Cheers.
>
>
> On Jan 18, 7:18�pm, sardinian_guy <gu...@xxxxxx-email.com> wrote:
Quote:

> > Hi guys. It's the first time that I try to use windows forms in
> > powershell. I'd like to add a browse button to choose a directory but
> > obviously my code doesn't work.
> > Someone can put me in the right way?
> >
> > [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
> > $form = new-object Windows.Forms.Form
> > $browse = new-object windows.Forms.OpenFileDialog
> > $browse.text = "..."
> > $form.controls.add($browse)
> > $form.ShowDialog()
> >
> > --
> > sardinian_guy
My System SpecsSystem Spec
Old 01-19-2009   #6 (permalink)
Shay Levy [MVP]


 
 

Re: add browse button to windows form

Hello BJ,

Do you actually see folders in the FolderBrowserDialog dialog? I guess not,
you need to launch PowerShell with -STA switch and it is
only available in v2. A workaround would be to use the Shell.Application
com object, check this thread:

http://stackoverflow.com/questions/2...rom-powershell


BTW, in XP the dialog appears behind the PowerShell window, you need to press
ALT+TAB to make it active.
You can also invoke the dialog without a form:

[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$browse = new-object system.windows.Forms.FolderBrowserDialog
$browse.RootFolder = [System.Environment+SpecialFolder]'MyComputer'
$browse.ShowNewFolderButton = $false
$browse.selectedPath = "C:\"
$browse.Description = "Choose a directory"

$browse.ShowDialog()
$browse.SelectedPath
$browse.Dispose()



---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar


B> Hi sardinian_guy,
B> you cannot ad the OpenFileDialog to the Form as a control.
B> Add a Button and asign the OpenFileDialog to the KlickEvent of the
B> Button:
B> [void][reflection.assembly]::LoadWithPartialName
B> ("System.Windows.Forms")
B> $form = new-object Windows.Forms.Form
B> $browse = new-object windows.Forms.OpenFileDialog
B> $browse.text = "..."
B> $button1 = New-Object system.Windows.Forms.Button
B> $button1.Text = "Choose...."
B> $button1.Add_Click({$browse.ShowDialog()})
B> $button1.left = 20
B> $button1.top = 20
B> $form.controls.add($button1)
B> $form.ShowDialog()
B> If you want to choose a Folder you should use the
B> FolderBrowserDialog. However i had problems with this in a Poweshell
B> V2 CTP3 Environment - it only runs in the ISE.
B>
B> Example:
B>
B> $button = $browse = $form = 0
B> [void][reflection.assembly]::LoadWithPartialName
B> ("System.Windows.Forms")
B> $browse = new-object system.windows.Forms.FolderBrowserDialog
B> $browse.RootFolder = [System.Environment+SpecialFolder]'MyComputer'
B> $browse.ShowNewFolderButton = $false
B> $browse.selectedPath = "C:\"
B> $browse.Description = "Choose a directory"
B> $button1 = New-Object system.Windows.Forms.Button
B> $button1.Text = "Choose Directory"
B> $button1.Add_Click({$browse.ShowDialog()})
B> $button1.left = 20
B> $button1.top = 20
B> $form = New-Object system.windows.forms.Form
B> $form.controls.add($button1)
B> $form.ShowDialog()
B> $form.Dispose()
B> $browse.SelectedPath
B>
B> Finally i do not recommend to use to much of this graphical stuff
B> with Powershell because it could be very extensive to arrange the
B> controls etc...
B> Using the Dialogs without the Form ist much easier.


My System SpecsSystem Spec
Old 01-19-2009   #7 (permalink)
BJ


 
 

Re: add browse button to windows form

Dear Shay and Marco,
thanks for the pointer to the STA Mode - i already tried this but it
didn't run.
My today's try worked - strange. Maybe it was too late
yesterday..... .

Best Regards,
BJ
My System SpecsSystem Spec
Old 01-19-2009   #8 (permalink)
bluefin


 
 

Re: add browse button to windows form

There is a saying by Shay Levy, one of PowerShell MVP says: " repeat
it? script it! ". That's about the intended purpose of PowerShell.

Cheers.

On Jan 19, 1:55*pm, tojo2000 <tojo2...@xxxxxx> wrote:
Quote:

> On Jan 18, 3:10*pm, bluefin <desertcamel.c...@xxxxxx> wrote:
>
>
>
>
>
Quote:

> > PowerShell is meant for console scripting rather than GUI, however,
> > you can use PrimalForms, a free GUI builder tool. It edits and stores
> > Windows Forms in a native XML format and generates PowerShell code on
> > demand. In addition, you must provide your own code e.g. function to
> > trigger the windows controls. Give it a try,http://www.primaltools.com/freetools/
>
Quote:

> > Cheers.
>
Quote:

> > On Jan 18, 7:18*pm, sardinian_guy <gu...@xxxxxx-email.com> wrote:
>
Quote:
Quote:

> > > Hi guys. It's the first time that I try to use windows forms in
> > > powershell. I'd like to add a browse button to choose a directory but
> > > obviously my code doesn't work.
> > > Someone can put me in the right way?
>
Quote:
Quote:

> > > [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
> > > $form = new-object Windows.Forms.Form
> > > $browse = new-object windows.Forms.OpenFileDialog
> > > $browse.text = "..."
> > > $form.controls.add($browse)
> > > $form.ShowDialog()
>
Quote:
Quote:

> > > --
> > > sardinian_guy
>
> Not necessarily. *PowerShell is for automation. *The majority of
> examples that people have are for the console, but PowerShell was made
> with more than this kind of automation in mind. *That being said, I'd
> second the suggestion to try PrimalForms. *You can write forms by hand
> as well, but that tool will make it much easier for you.
My System SpecsSystem Spec
Old 01-21-2009   #9 (permalink)


Vista Home Premium 32bit
 
 

Re: add browse button to windows form

Hi guys, thanks to everybody for all your answers.
I spent a lot of time to try all your advices. I didn't think it was so difficult. I'm using windows xp pro sp3 and my powershell version is 1. The link posted by Shay work

$app = new-object -com Shell.Application
$folder = $app.BrowseForFolder(0, "Select Folder", 0, "C:\")
if ($folder.Self.Path -ne "") {write-host "You selected " $folder.Self.Path}


However my hard disk has several units. Is it possible to browse them all or I have to specify one unit at time?
Moreover if I add a simple

gci $folder -rec after those three lines nothing happens.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
New System - Vista Home Premium - Not able to browse microsoft sites- able to browse other sites. Vista networking & sharing
can facebook browse button open photo gallery instead of explorer? Vista music pictures video
Browse button not working with any application Vista General
Browse button not working with any application Vista file management
Cannot browse Internet Sites with IE7+ and Vista b2, Can browse LAN sites Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46