Tuesday, May 18, 2010

WPK Snippets

Here is another WPK snippet for you. This one will demonstrate how to use New-Slider and New-Ellipse. Assuming that you the PowerPack Modules installed, you should be able to cut and paste the following:
Import-Module WPK           
New-Window -Name "WPK" -Title "WPK Snippets" -Width 250 -Height 175 -Show {
New-Grid -Rows 28,100 -Columns 200* {
New-TextBox -Name txtSliderValue -Column 0 -Row 1
$fill = [System.Windows.Media.Brushes]::Blue
$stroke = [System.Windows.Media.Brushes]::Black
New-Ellipse -Name oEllipse -Column 0 -Row 2 -Height 100 -Width 100 `
-Fill $fill -Stroke $stroke
New-Slider -Name Slider -Column 0 -Row 0 -Minimum 0 -Maximum 200 `
-IsSnapToTickEnabled -On_ValueChanged {
$txtSliderValue = $window | Get-ChildControl txtSliderValue
$slider = $window | Get-ChildControl Slider
$oEllipse = $window | Get-ChildControl oEllipse
$oEllipse.width = $slider.value
}
}
}
You should see something like the following:


Moving the slider triggers the -On_ValueChanged event.
Moving it around you should see the width of the ellipse change accordingly.


Download the script.

Enjoy!

Monday, May 10, 2010

WPK Snippets

I have been experimenting with the WPK module from the PowerShell PowerPack. This module enables you to easily write user interfaces within PowerShell. Think PrimalForms but with less code and staying within one IDE. The hard part of writing the UI in WPK is knowing all the methods and properties associated with a particular object. I find searching MSDN for the members very helpful. As I use the different objects (controls), I will share my findings. Up first is the New-FolderBrowserDialog. These examples are ready to be cut and pasted into PowerShell ISE.
Import-Module WPK
New-Window -Name "WPK" -Title "WPK Snippets" -Width 400 -Height 100 -Show {
New-Grid -Rows 28,28 -Columns 75,300* {
New-TextBlock -Text "Folder Path:" -Row 1 -Column 0 -Margin 3
New-TextBox -Name txtUserName -Width 300 -Row 1 -Column 1 -Margin 3
New-Button -Content "Folder" -Name btnFolder -Row 0 -Column 1 -Margin 3 -Width 300 -On_Click {
$obj = New-FolderBrowserDialog -RootFolder "MyComputer"
$obj.ShowDialog()
$txtUserName = $Window | Get-ChildControl txtUserName
$txtUserName.Text = $obj.SelectedPath }
}
}

You should see something similar to the following:


Clicking on the button brings up the familiar "Browse For Folder" Dialog box.


Select a folder and the path is placed in the appropriate textbox.


Enjoy!