Joomla TemplatesWeb HostingWeb Hosting
14
Oct
Events in PoshBoard 2.0 Part 1 E-mail

PoshBoard binds events to different kinds of controls, these events transform Silvelright items on your page via PowerShell script result...

Events are able to invoke script-based commands when user interacts with your portal. As for now, 3 controls propose events (this will be extended in future updates), and every Silverlight object are possible targets for updates.

The "event-enabled" controls are :

  • Button > Click
  • ComboBox > SelectionChanged
  • ListBox > SelectionChanged
Of course every controls will potentially manage every kind of events managed by Silverlight and WPF, but I decided to keep it simple for the first release.

PBElement object exposes a property called Events. It's a List of PBEvent object. A PBEvent object has 5 properties :
  • Source: object name binded to this event. This is automatically set when you bind an event to a control.
  • Name: name of the event. Click, SelectionChanged and so on..
  • Target: PBElement object (name string or PBElement object variable) that will be updated by the event script.
  • Script: the script or script reference that will be launched by the event.
  • References: PBElement (name string or PBElement object) that can update the event script before launch.
We will now go a bit deeper in this, specially on Script, Target and References properties because -Source and -Name  are properties easy to understand.

Sample 1: basic event creation

In this example we will create a basic layout with a grid that contains a button and a textbox. When you click on the button, a Get-Process script is invoked and the result appears in the textbox.

$outputbox = New-PBTextbox -Row 0 -Name output
$button = new-pbbutton -row 1 -text "Press Me" -fontsize 14
add-pbevent -input $button -name click -script "Get-Process" -target $outputbox
 
$grid = new-pbgrid -columns ("1*") -Rows ("1*",50)
add-pbitem $grid ($outputbox,$button)
 
$grid

You can see here that I added an event to the button.

add-pbevent -input $button -name click -script "Get-Process" -target $outputbox

I invoke the "click" event, I add "Get-Process" as script code and specify that the control $outputbox is my target for script result.

The result will depend of the target type. PoshBoard analyzes your dynamic controls and adapt the script result : if I put a Chart object as a target, PoshBoard would take your returning PBElement object and update the chart with the ChartSeries in your PBelement object.

Sample 2 : Chart target with multiline event script

Here's another example with this basic tehcnique. Here I need to put a script on several line, longer than a simple "get-process". You're able to use a string variable in PowerShell to put multiline scripts :

$script = @'
$files = dir C:\windows|sort length|select -first 10
$files|out-pbchart length -dpnames Name -RenderAs bar -Name "FileSize" -Title "Files Size"
'@
 
$grid = New-PBGrid -Columns ("1*") -Rows ("1*",50)
$chart = New-PBChart -Name "OutChart"
$button = new-pbbutton -row 1 -text "Press Me" -fontsize 14
 
add-pbevent -inputobject $button -name click -script $script -target $chart
add-pbitem $grid ($chart,$button)
$grid

As you can see, PoshBoard uses now the $script variable in -Script parameter.

I just defined a chart as a target instead of the textbox, and PoshBoard knows that the script return a "chart" PBElement, and uses it's content (properties, chartseries) to update the target. Of course, if your event's script doesn't return a PBElement of type "Chart", an error will occur.

In the next post, we'll discover more advanced event handling, dynamic script modification with Referencesproperty and other way to use / reuse part of the main code in your events.

Comments
Search
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated on Thursday, 15 October 2009 09:31