Simple UI to browse and select from windows.folder structure?

Jan 17, 2013 at 5:09 PM

I may be missing it. I am new at powershell and ShowUI I have gone threw the examples. How would I call the default

 

Windows.Forms.FolderBrowserDialog

 

In ShowUI and incorporate that into my UI. I appreciate any pointers.

Coordinator
Jan 18, 2013 at 6:25 AM

Well, I should note that ShowUI is WPF, and that is Windows.Forms ... but in general, you should be able to just

 

add-type -as System.Windows.Forms
$d = new-object Windows.Forms.FolderBrowserDialog
if($d.ShowDialog() -eq "OK") { 
   <# stuff that requires the folder goes here #>
   $path = $d.SelectedPath
}

 

Here's a more complete working example:

 

Import-Module ShowUI
# You don't have to do load the assembly, ShowUI did it already
# Add-Type -AssemblyName System.Windows.Forms

Window -Width 200 -Height 130 {
  Grid -Rows Auto, Auto, Auto -Margin 5 { 
    Label "Please specify a folder:"
    Grid -Row 1 -Columns *, Auto -Margin 5 { 
      TextBox -Name Path -MinWidth 100
      Button " ... " -Column 1 -On_Click {

        # Here is how you use FolderBrowserDialog:
        # You don't want to make a new one each time
        if(!$this.Tag) {
          $this.Tag = new-object Windows.Forms.FolderBrowserDialog
        }
        
        # You might want to pre-select a path, here's how:
        if($Path.Text) {
          $this.Tag.SelectedPath = $Path.Text
        }

        if($this.Tag.ShowDialog() -eq "OK") { 
          # They chose a folder, stick it in our UI
          $Path.Text = $this.Tag.SelectedPath
        }

      }
    }
    Button "OK" -Row 2 -Width 80 -IsDefault -HorizontalAlignment Right -Margin 0,0,10,0 -On_Click {
        Set-UIValue $window -passThru | Close-Control
    }
  }
} -Show

 

 

Jan 18, 2013 at 3:10 PM

Jaykul,

 

Thanks for all your help in this and the other thread. I am dense on this subject I am guessing. When I run the above code I get nothing but a blank box.  I follow most of what you are doing besides the

if(!$this.Tag)

Not fully understanding that call. Again I appreciate your patients with this heavy noob :(

Coordinator
Jan 18, 2013 at 7:37 PM

A blank box!?  I tested that script on two systems, and on ShowUI 1.3 (and 1.4 which I see now was not properly released, bah James!) just to be sure.

In this script, I want to only create a new FolderBrowserDialog if I haven't already done so (that is, if you click the button twice I want to reuse the dialog from before, instead of creating a new one). So I need to store the dialog in a variable where I can be sure it will survive when the event handler script exists.

The $this variable is a magic variable in ShowUI's event handlers, it refers to the element that triggered the event (in this case, the button).  I know it's always going to be set to the button when this event handler runs.

Every WPF element object has a .Tag property.  It's null by default, but you can put anything in it.  We use it for the UIValue stuff, as a matter of fact, so you should be careful about where you're setting it (in this case, I set it on a Button, which doesn't have a value, so I know setting the .Tag isn't going to screw up Get-UIValue later).

So basically:

 

# If the button's tag property is not set
if(!$this.Tag) {
    # Then set the tag to hold our new FolderBrowserDialog
    $this.Tag = new-object Windows.Forms.FolderBrowserDialog
}

From then on we just use $this.Tag when we want to use the dialog.

 

Jan 24, 2013 at 1:08 AM

Ya I am on 1.3 and windows 7 64 bit powershell 2.0. I have tried on to seperate computers one running powershell 2.0 and one running 3.0. Using the above code all I get is a blank white box that I can close.

Coordinator
Jan 24, 2013 at 4:09 AM
Edited Jan 24, 2013 at 4:09 AM

I don't know why your window is blank.  I could force set bounds instead:

Window -Title "Choose a Folder" -Width 257 -Height 130 {
  Grid -Rows 26, 26, 32 -Margin 5 { 
    Label "Please specify a folder:"
    Grid -Row 1 -Columns 195, 36 -Margin 5,5,0,5 { 
      TextBox -Name Path -Padding 2,2,0,0
      Button " ... " -Column 1 -On_Click {

        # Here is how you use FolderBrowserDialog:
        # You don't want to make a new one each time
        if(!$this.Tag) {
          $this.Tag = new-object Windows.Forms.FolderBrowserDialog
        }
        
        # You might want to pre-select a path, here's how:
        if($Path.Text) {
          $this.Tag.SelectedPath = $Path.Text
        }

        if($this.Tag.ShowDialog() -eq "OK") { 
          # They chose a folder, stick it in our UI
          $Path.Text = $this.Tag.SelectedPath
        }

      }
    }
    Button "OK" -Row 2 -Width 80 -Height 26 -IsDefault -VerticalAlignment Center -HorizontalAlignment Right -On_Click {
        Set-UIValue $window -Passthru | Close-Control
    }
  }
} -Show