<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; PowerBoots</title>
	<atom:link href="http://joelbennett.net/tag/powerboots/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelbennett.net</link>
	<description>The internet home of Joel "Jaykul" Bennett...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='joelbennett.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Disabling Events in ShowUI</title>
		<link>http://joelbennett.net/disabling-events-in-showui/</link>
		<comments>http://joelbennett.net/disabling-events-in-showui/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 18:35:20 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WinForms]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1701</guid>
		<description><![CDATA[Because some things just work better in WPF. Let&#8217;s say you had a form that collected a bunch of user input, and then had a button that would fire off some work. We&#8217;ll assume that you wanted to prevent people from firing off the work again before they know the results of the first time, [...]]]></description>
			<content:encoded><![CDATA[	<h1>Because some things just work better in <span class="caps">WPF</span>.</h1>

	<p>Let&#8217;s say you had a form that collected a bunch of user input, and then had a button that would fire off some work.  We&#8217;ll assume that you wanted to prevent people from firing off the work again before they know the results of the first time, so you&#8217;ll disable the button while you&#8217;re working. Something like this (leaving out the boring stuff about collecting the user&#8217;s input and displaying the results):</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> ShowUI<br />
<br />
StackPanel <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;TextBlock <span style="color: #000066;">-Name</span> Output<br />
&nbsp; &nbsp;Button <span style="color: #009900;">&quot;Click Me&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">3</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the user about what we're doing:</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Simulate doing some hard work ...</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">3</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span> <br />
&nbsp;</div>

	<p>It&#8217;s pretty simple. In fact, I think there&#8217;s only three things worth pointing out:</p>

	<ol>
		<li>There are variables for named controls.</li>
		<li>If you want to make sure a control has time to update it&#8217;s display while you&#8217;re executing your event handler, call UpdateLayout().</li>
	</ol>
	<ol>
		<li>You can disable and enable buttons.</li>
	</ol>

	<h3>Now, try doing the same thing with Windows Forms.</h3>

	<p>This was <em>my</em> first attempt:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>void<span style="color: #333;">&#93;</span><span style="color: #333;">&#91;</span>reflection.<span style="color: #003366;">assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadWithPartialName</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;System.Windows.Forms&quot;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Create the form</span><br />
<span style="color: #660033; font-weight: bold;">$form</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">Form</span><br />
<span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">Size</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;250,250&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create text</span><br />
<span style="color: #660033; font-weight: bold;">$output</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">label</span><br />
<span style="color: #660033; font-weight: bold;">$output</span>.<span style="color: #003366;">Anchor</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Top,Left,Right&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$output</span>.<span style="color: #003366;">AutoSize</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create button</span><br />
<span style="color: #660033; font-weight: bold;">$button</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">button</span> <br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Anchor</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Bottom,Right&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click Me&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Location</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;150,180&quot;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Add_Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## Simulate doing work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">5</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">AddRange</span><span style="color: #333;">&#40;</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$output</span>, <span style="color: #660033; font-weight: bold;">$button</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## show the form</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>void<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">showdialog</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>Now, I bet you noticed that took a bit more code than the <span class="caps">WPF</span> version in <a href="http://ShowUI.CodePlex.com">ShowUI</a>, but the thing about it is that I also had to manually specify the positions of the controls (and I had to set the size of the form to make that work). And after all that, it still doesn&#8217;t actually work, and it has a few ugly behaviors:</p>

	<p>First of all, disabling the control doesn&#8217;t work, because Windows posts all click events as messages to the button&#8217;s message queue, and it doesn&#8217;t know that it should ignore them, because it only processes one at a time &#8212; when it&#8217;s done running the Click handler the first time, it notices there&#8217;s another event &#8230; and since it&#8217;s enabled, it processes it.  If you click it five times while it&#8217;s disabled, you&#8217;re going to get the work done five more times&#8230; eventually.</p>

	<p>On top of that, although the label updates without being told to, it didn&#8217;t push the button down the screen or resize the form, so eventually it overlaps the button, passes through the bottom of the form and disappears.</p>

	<p>The next thing I tried was to actually unhook the event handler before I disable the button &#8230; but that had no effect either, because (as I wrote earlier) the button doesn&#8217;t actually ignore the clicks while it&#8217;s doing the work &#8212; it just queues them up for processing later.</p>

	<p>Of course, I could do the work in a background job so that the button would return immediately, but that doesn&#8217;t meet the requirements I have for not queuing up extra work before the first work is done, and in fact, then I&#8217;d have extra work to do to create a time to check and see if the remote job was finished or not.</p>

	<p>[New] Thanks to <a href="http://huddledmasses.org/disabling-events-in-showui/comment-page-1/#comment-215538">SAPIENDavid</a>, I&#8217;ve realized the simple fix for the disabling problem, we just have to call <code>DoEvents</code> to empty the event queue <em>before</em> we re-enable the button.</p>

	<p>Here&#8217;s what <strong>did</strong> work:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> <span style="color: #000066;">-Assembly</span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Create the form</span><br />
<span style="color: #660033; font-weight: bold;">$form</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">Form</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Size <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;200,70&quot;</span><br />
&nbsp; &nbsp; AutoSize <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span> <br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Create a FlowLayout</span><br />
<span style="color: #660033; font-weight: bold;">$panel</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">FlowLayoutPanel</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Dock <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Fill&quot;</span><br />
&nbsp; &nbsp; FlowDirection <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;TopDown&quot;</span><br />
&nbsp; &nbsp; AutoSize <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create text</span><br />
<span style="color: #660033; font-weight: bold;">$output</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">label</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Dock <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Fill&quot;</span><br />
&nbsp; &nbsp; Text <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click the button when you're ready to work.<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; AutoSize <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create button</span><br />
<span style="color: #660033; font-weight: bold;">$button</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">button</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Anchor <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Bottom,Right&quot;</span><br />
&nbsp; &nbsp; Text <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click Me&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$lastButtonClick</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">get-<span style="font-style: normal;">date</span></span><br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Add_Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## Simulate doing work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">5</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#Process the pending messages before enabling the button</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">Application</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DoEvents</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$panel</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">AddRange</span><span style="color: #333;">&#40;</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$output</span>, <span style="color: #660033; font-weight: bold;">$button</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">AddRange</span><span style="color: #333;">&#40;</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$panel</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## show the form</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>void<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">showdialog</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>To fix the other problems, I added a FlowLayoutPanel (which is very different from the StackPanel in <span class="caps">WPF</span>, but still serves the same purpose), and made all the relevant bits autosize (it&#8217;s always surprising to me when I need to drop back to WinForms, how everything has to be <strong>told</strong> to autosize and fill empty space). </p>

	<p>That&#8217;s enough to take care of the output problem, and make the two solutions roughly equivalent (there&#8217;s still some differences, as you can see if you run them).</p>

	<p>For what it&#8217;s worth, this was a real world question from a user at our <a href="http://PowerShellGroup.org/virtual/live">Virtual User Group</a> this morning, and I just couldn&#8217;t help sharing how much easier user interfaces are to write in ShowUI. Clearly a designer like PrimalForms makes laying out the controls easier &#8212; but when it comes to the little things, you still have to figure out to make them work, and actually implement your event handlers correctly.</p>

	<p>Just for the record, here&#8217;s what it would take to implement the <span class="caps">WPF</span> solution without ShowUI:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> <span style="color: #000066;">-Assembly</span> PresentationFramework<br />
<span style="color: #660033; font-weight: bold;">$window</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Window</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; SizeToContent <span style="color: #66cc66;">=</span> <span style="color: #009900;">'WidthAndHeight'</span><br />
&nbsp; &nbsp; Content <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">StackPanel</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #660033; font-weight: bold;">$output</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">TextBlock</span><br />
<span style="color: #660033; font-weight: bold;">$button</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">Button</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Content <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click Me&quot;</span><br />
&nbsp; &nbsp; Margin <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">3</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Add_Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the user about what we're doing:</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">UpdateLayout</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Simulate doing some hard work ...</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">3</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">Children</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$output</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">Children</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$button</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">ShowDialog</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/disabling-events-in-showui/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ShowUI: Handling Events and Producing Output</title>
		<link>http://joelbennett.net/showui-handling-events-and-producing-output/</link>
		<comments>http://joelbennett.net/showui-handling-events-and-producing-output/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 05:22:10 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WinForms]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1694</guid>
		<description><![CDATA[Once you get past the basics of WPF and ShowUI, learning to use nested panels or grids to achieve the layouts you want, and start getting a grip on what controls are available by default, the next step to building useful user interfaces is going to be handling user interactions. In programming, we call those [...]]]></description>
			<content:encoded><![CDATA[	<p>Once you get past the basics of <span class="caps">WPF</span> and <a href="http://showui.codeplex.com/">ShowUI</a>, learning to use nested panels or grids to achieve the layouts you want, and start getting a grip on what controls are available by default, the next step to building useful user interfaces is going to be handling user interactions.</p>

	<p>In programming, we call those interactions &#8220;events.&#8221; An event in <span class="caps">WPF</span> covers all forms of user interactions like clicking a button, type in a textbox, or select items from a treeview or listbox, and even less obvious things like when the right or left mouse button get&#8217;s pressed down, or let up &#8230; or when the mouse moves, of when the control gets focus or looses it, or &#8220;touch&#8221; and &#8220;stylus&#8221; events, drag-and-drop events, etc.  There are also events that aren&#8217;t caused by users, like events for when databinding is updated, when the control is initialized, hidden, made visible, etc.</p>

	<p>In ShowUI, all events are handled by assigning scriptblocks to a parameter who&#8217;s name starts with &#8220;On_&#8221; like -On_Click or -On_GotFocus or -On_MouseLeftButtonDown or -On_TextInput &#8230; and so on.</p>

	<p>Let&#8217;s say that you want a quick dialog like this:</p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-10.png" class="float-right-block" alt="" width="225" height="118" /></p>

	<p>You&#8217;re going to need to handle the OK button click, of course, but in that scriptblock, you&#8217;re going to want to get the text from the textbox, and make sure that it gets returned when the window is closed &#8230; and you&#8217;re going to want to close the Window! </p>

	<p>We&#8217;re here to help! Within the event handler script blocks, ShowUI defines a bunch of variables for you to help you handle the event: <code>$this</code> is the source of the event, <code>$_</code> is the event arguments, and <code>$window</code> is the top-level window that contains your UI. Any named controls in your script are also exposed as variables, so if you started with this script:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
StackPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">&quot;Prompt&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,8,8&quot;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Please Enter Your Full Name:&quot;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-Name</span> FullName <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-IsDefault</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">50</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,0,0&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Do something to output the name!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>You&#8217;re going to be able to get the text from the TextBox using <code>$FullName.Text</code> because you know the TextBox has a property named &#8220;Text&#8221; (since it&#8217;s exposed as a parameter for you in the TextBox command), and because now you know that ShowUI creates a variable for all the named controls.</p>

	<p>In order to write output from a script like that one, you have to set the <code>Tag</code> property on the top level control (in this case, the StackPanel, which is obviously named &#8220;Prompt&#8221;). You can do that easily by hand, or you can use the Set-UIValue function.</p>

	<p>In order to close the window, you&#8217;re going to have to do one of two things: first, you can use the handy <code>Close-Control</code> function, or you can call the Close method on the window. The Close-Control function will look at the &#8220;parent&#8221; (and it&#8217;s parent) to try and find the window that needs to be closed &#8212; but if it can&#8217;t find one, it will just <strong>hide</strong> the parent, so if your button were several layers deep (unlike ours), you&#8217;d have to specify the top level control as a parameter.</p>

	<p>Here&#8217;s two versions of what it could have looked like when I was finished:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
StackPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">&quot;Prompt&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,8,8&quot;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Please Enter Your Full Name:&quot;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-Name</span> FullName <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-IsDefault</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">50</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,0,0&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Prompt</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$FullName</span>.<span style="color: #003366;">Text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$FullName</span>.<span style="color: #003366;">Focus</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<div class="posh code posh" style="font-family:monospace;"><br />
StackPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">&quot;Prompt&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,8,8&quot;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Please Enter Your Full Name:&quot;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-Name</span> FullName <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-IsDefault</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">50</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,0,0&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">UIValue</span></span> <span style="color: #660033; font-weight: bold;">$Prompt</span> <span style="color: #000066;">-Passthru</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Close-<span style="font-style: normal;">Control</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$FullName</span>.<span style="color: #003366;">Focus</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>One thing you&#8217;ll notice right away is that I cheated and actually added another event handler too: For the &#8220;Loaded&#8221; event on the StackPanel.  This event handler is called during the initialization of the user interface, and gives you a chance to do things like what I did here: set the initial keyboard focus where you want it (so the user can start typing as soon as the window pops up).</p>

	<p>However, if you run them both, you&#8217;ll notice another thing: the output is different.  In the second example I took advantage of the fact that Set-UIValue will actually call Get-UIValue if you don&#8217;t pass it a parameter!  The cool thing about Get-UIValue is that if it doesn&#8217;t find a &#8220;Tag&#8221; on the specified control, it will look through the children to find one, and create a hashtable out of the values it finds. So in this case, rather than write the code to get the value from the right textbox and set it myself, I just let the built-in features of ShowUI do their thing.</p>

	<h3>A bigger example</h3>

	<p>Of course, in neither example did I need to do anything with the button or with the actual parameters that are passed in to the button&#8217;s &#8220;Click&#8221; event &#8230; so perhaps one last (more complicated) example would be useful:</p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-11.png" class="float-right-block" alt="" width="185" height="167" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Grid</span></span> <span style="color: #000066;">-ControlName</span> SelectUserGroups <span style="color: #000066;">-Columns</span> Auto,<span style="color: #66cc66;">*</span> <span style="color: #000066;">-Rows</span> <span style="color: #cc66cc;">4</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$GetGroups</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADUuser</span></span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-SizeLimit</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">LogonName</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-or</span> <span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">Email</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Black&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADGroup</span></span> <span style="color: #000066;">-ContainsMember</span> <span style="color: #660033; font-weight: bold;">$user</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$UserName</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">LogonName</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$EmailAddress</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">Email</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Red&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Label</span></span> <span style="color: #009900;">&quot;Name&quot;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Textbox</span></span> <span style="color: #000066;">-name</span> UserName <span style="color: #000066;">-minwidth</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-On_LostFocus</span> <span style="color: #660033; font-weight: bold;">$GetGroups</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Label</span></span> <span style="color: #009900;">&quot;Email&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Textbox</span></span> <span style="color: #000066;">-name</span> EmailAddress <span style="color: #000066;">-minwidth</span> <span style="color: #cc66cc;">100</span> &nbsp;<span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">1</span> &nbsp;<span style="color: #000066;">-On_LostFocus</span> <span style="color: #660033; font-weight: bold;">$GetGroups</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Label</span></span> <span style="color: #009900;">&quot;Group&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Listbox</span></span> <span style="color: #000066;">-name</span> <span style="color: #660033;">Group</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Button</span></span> <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">3</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ParentControl</span></span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">UIValue</span></span> <span style="color: #000066;">-Passthru</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Close-<span style="font-style: normal;">Control</span></span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>Hopefully, by now this doesn&#8217;t need a whole lot of explanation, but let&#8217;s walk through it anyway.  First of all, if you&#8217;re not familiar with them, this script uses the excellent <a href="http://www.quest.com/powershell/activeroles-server.aspx">PowerShell Commands for Active Directory</a> from Quest software.  This doesn&#8217;t represent a full, complete, useful user interface &#8212; it&#8217;s more of an example that you can hopefully work from (and please, contribute ammendments <a href="http://poshcode.org/2737">on PoshCode</a>).</p>

	<p>You can see that I used a Grid with four rows and two Columns: with one column &#8220;Auto&#8220;sized, and one using up all the rest of the UI (you can resize the window to make the fields bigger).  The first thing in the grid is the definition of a scripblock which I assigned to a variable <code>$GetGroups</code>.  The reason I did that is because I wanted to reuse the scriptblock as the event handler for both the UserName and EmailAddress fields.</p>

	<p>Without dwelling a whole lot on it, you can see the last line is the &#8220;OK&#8221; button which get&#8217;s the parent grid and calls Set-UIValue to invoke, as before, the hashtable collection of all the textbox values.</p>

	<p>The interesting stuff is in that GetGroup event handler:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADUuser</span></span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-SizeLimit</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">LogonName</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-or</span> <span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">Email</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Black&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADGroup</span></span> <span style="color: #000066;">-ContainsMember</span> <span style="color: #660033; font-weight: bold;">$user</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$UserName</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">LogonName</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$EmailAddress</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">Email</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Red&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>You can see that first we call <code>Get-QADUser</code> with the Text of <code>$this</code> field.  By using <code>$this</code>, we make it so the event handler will work on both Textboxes, since it will get the text of whatever triggered the event handler.  Get-QADUser doesn&#8217;t return anything unless it finds a user, and setting the SizeLimit ensures that we won&#8217;t end up waiting for it to retrieve &#8220;all&#8221; the users just because the Textbox was left empty.  In fact, the point of that line is to make sure that we matched a user.</p>

	<p>On the next line, I&#8217;m making sure that either the LogonName or Email of the user that we found matches fully the text that the user typed. This makes sure that the user that we matched is a full match, so we know we&#8217;ve gotten the person typing into the form to type a full, complete username or email address.</p>

	<p>When it does match, we set the color to Black (in case it was an error and set to Red before), and we call Get-QADGroup to get all the groups that the user is a member of.  We set those groups as the source for the <code>$Group</code> listbox, and they&#8217;ll immediately show up for the user.  And finally, we update the few fields we&#8217;re showing from the user object we retrieved earlier. </p>

	<p>Of course, when it doesn&#8217;t match, we set the text red to indicate an error, and then we zero out the data on the group listbox.</p>

	<p>I hope this has helped some of you figure out event handlers in ShowUI &#8212; please feel free to ask questions in the comments below or on the <a href="http://showui.codeplex.com/discussions">ShowUI discussion boards</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/showui-handling-events-and-producing-output/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ShowUI: the tutorial walkthrough</title>
		<link>http://joelbennett.net/showui-tutorial-walkthrough/</link>
		<comments>http://joelbennett.net/showui-tutorial-walkthrough/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 04:44:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WalkThrough]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1679</guid>
		<description><![CDATA[Ok, let&#8217;s be clear: you&#8217;ve seen this before, and this isn&#8217;t going to be the last you&#8217;ll see of it, because we&#8217;ve got much more in store in ShowUI, but for the first release of ShowUI, it&#8217;s obviously time to update this simple walkthrough of building simple user interfaces in PowerShell! An introduction to ShowUI [...]]]></description>
			<content:encoded><![CDATA[	<p>Ok, let&#8217;s be clear: you&#8217;ve seen this <a href="/powerboots-tutorial-walkthrough">before</a>, and this isn&#8217;t going to be the last you&#8217;ll see of it, because we&#8217;ve got much more in store in <a href="http://showui.codeplex.com">ShowUI</a>, but for the first release of ShowUI, it&#8217;s obviously time to update this simple walkthrough of building simple user interfaces in PowerShell!</p>

	<h2>An introduction to ShowUI</h2>

	<p>ShowUI is the next generation PowerShell module for building user interfaces in script. It&#8217;s the merger of my previous PowerShell UI project (called PowerBoots) with one by James Brundage, former Microsoft PowerShell team member and founder of <a href="http://start-automating.com">Start-Automating</a> (called <span class="caps">WPK</span>) which shipped in the Windows 7 resource kit.</p>

	<h4>If you want to follow along, you need to:</h4>

	<p>1. Get a copy of the <a href="http://showui.codeplex.com/releases/">ShowUI Module</a> from CodePlex <br />
2. Install it by putting the &#8220;ShowUI&#8221; folder in one of your &#8220;Modules&#8221; folders (list them by typing <code>$Env:PSMODULEPATH</code> in PowerShell v2, and feel free to create the one you want if it doesn&#8217;t exist). You should end up with something like <code>C:\Users\Jaykul\Documents\WindowsPowerShell\Modules\ShowUI\ShowUI.psm1</code><br />
3. Run PowerShell <span class="caps">ISE</span>, or use PowerShell.exe with the -<span class="caps">STA</span> switch (the best way to do this is to add it to the shortcut you use to launch PowerShell: open the properties dialog, and on the Shortcut tab, add &#8220; -STA&#8221; to the Target)<sup class="footnote"><a href="#fn1">1</a></sup>.</p>

	<p>Did I hear someone ask <strong>what <em>is</em> WPF?</strong> It was introduced as part of .Net 3.0 (and vastly improved in .Net 3.5, and again in 4.0), so you can expect to find it preinstalled on computers from Vista on, and of course you can download and install it on XP if it&#8217;s not already installed.  The only thing you really need to know about <span class="caps">WPF</span> for the purposes of this tutorial is that it is <strong>the</strong> new <span class="caps">GUI</span> toolkit for .Net, and that it is container based &#8212; you put elements into other elements to control the layout, rather like <span class="caps">HTML</span> and Java Swing&#8230; you can <strong>pick up the rest as we go along</strong>.</p>

	<h2>A simple ShowUI program</h2>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-01.png" class="float-right-block" alt="" width="167" height="85" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Button</span></span> <span style="color: #000066;">-Content</span> <span style="color: #009900;">&quot;Hello World&quot;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>This first example is a little bit more verbose than it needs to be, because the <code>-Content</code> parameter is positional, so the first non-named argument you pass will be used for that. The same is true for the -Children parameter of panels, and in fact, each of the other similar content parameters: Items, Blocks, and Inlines.</p>

	<p>Additionally, each control is aliased without the New- verb, so you could just call <code>Button</code> instead of <code>New-Button</code> &#8230; and of course, since our button doesn&#8217;t do anything, we could just as easily have used a Label, and written:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Label <span style="color: #009900;">&quot;Hello World&quot;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>Note: &#8220;Label&#8221; is also the name of an executable for labelling drives in Windows, make sure ShowUI is imported before you run that command.</p>

	<p>One last point: ShowUI brings a lot of features from <span class="caps">WPK</span> to the table, and one you&#8217;ll use a lot is the ability to skip specifying the window and put the -Show parameter on almost any <span class="caps">WPF</span> element. This is partly because we&#8217;re back to running PowerShell 2.0 with the -<span class="caps">STA</span> switch, but in any case, we can now skip the &#8220;New-BootsWindow&#8221; command, and our examples can be that much simpler.</p>

	<h2>We can put controls in a stack</h2>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-02.png" class="float-right-block" alt="" width="165" height="132" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">UI</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A bed of clams&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A gulp of swallows&quot;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>StackPanels are awesome. So are WrapPanels. Try that code with a WrapPanel instead of a StackPanel and see what the difference is (hint: try resizing the window). Actually, try it with a UniformGrid too. <span class="caps">WPF</span> has several other panel containers too: Grid, ToolBarPanel, TabPanel, DockPanel, Canvas &#8230; we&#8217;ll talk about some more of those later.</p>

	<h2>Ok, lets see some formatting</h2>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-03.png" class="float-right-block" alt="" width="256" height="179" /></p>

	<p>To scoot the buttons out from the edge we can use margins or padding: margins go on the outside of containers, padding goes on the inside. We can also specify the FontFamily, FontSize, FontWeight, and FontStyle, as well as Foreground and Background colors &#8230; </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">UI</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Background</span> Pink <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span> <span style="color: #000066;">-FontFamily</span> Consolas <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-FontWeight</span> Bold<br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span> <span style="color: #000066;">-FontFamily</span> Arial <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">20</span> <span style="color: #000066;">-FontStyle</span> Italic<br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #000066;">-FontFamily</span> <span style="color: #009900;">'Segoe UI'</span> <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">18</span> <span style="color: #000066;">-Foreground</span> Crimson<br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>So you see, the pink background is on the StackPanel, which has a (default, white) margin around it.  If you wanted the whole background of the window to be pink, you would need to set the background of the Window instead of the StackPanel.</p>

<h3 style="clear: both;">An aside on Typography</h3>

	<p>ShowUI doesn&#8217;t need to create a full set of typography-specific top-level elements the way Shoes does, because we are based on <span class="caps">WPF</span>, which has a far more powerful typography system available than any we&#8217;ve used previously. So, with <span class="caps">WPF</span> we start by selecting a control based on how much text you want to put in it, and how much formatting you want to apply: Label and TextBox are the simplest, TextBlock supports limited text formattings, and FlowDocumentViewer or RichTextBox support full rich content. And of course, Hyperlink supports clicking  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  </p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-07.png" class="float-right-block" alt="" width="507" height="186" /></p>

	<p>For the typography elements, the content model changes a little bit.  There are basically two types of typographical elements: Inline and Block elements (where inline elements can&#8217;t contain block elements). In fact, he <a href="http://msdn.microsoft.com/en-us/library/bb613554.aspx">TextBlock Content Model</a> is similar to that of an inline element. It is actually a type-restricted &#8220;Items&#8221; container.  Instead of being able to have <em>anything</em> as content, it can only contain <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.inline.aspx">Inline</a> flow content elements such as <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.anchoredblock.aspx">AnchoredBlock</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.bold.aspx">Bold</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.hyperlink.aspx">Hyperlink</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.inlineuicontainer.aspx">InlineUIContainer</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.italic.aspx">Italic</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.linebreak.aspx">LineBreak</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.run.aspx">Run</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.span.aspx">Span</a>, and <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.underline.aspx">Underline</a>, and it will create a run automatically if you just put a text string in it. </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">UI</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">10</span> <span style="color: #000066;">-Children</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #009900;">&quot;A Question&quot;</span> <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">42</span> <span style="color: #000066;">-FontWeight</span> Bold <span style="color: #000066;">-Foreground</span> <span style="color: #009900;">&quot;#FF0088&quot;</span> <br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-Inlines</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bold <span style="color: #009900;">&quot;Q. &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Are you starting to dig &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Hyperlink <span style="color: #009900;">&quot;ShowUI?&quot;</span> <span style="color: #000066;">-NavigateUri</span> http:<span style="color: #66cc66;">//</span>huddledmasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>tag<span style="color: #66cc66;">/</span>showui<span style="color: #66cc66;">/</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-On_RequestNavigate</span> <span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">NavigateUri</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">16</span> <span style="color: #000066;">-Inlines</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Span <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-FontWeight</span> Bold <span style="color: #000066;">-Inlines</span> <span style="color: #009900;">&quot;A. &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Leave me alone, I'm hacking here!&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Note: If you want support for the full <a href="http://msdn.microsoft.com/en-us/library/aa970909.aspx">document model</a> (which allows Paragraphs and Lists), you need to use a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentreader.aspx">FlowDocumentReader</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentpageviewer.aspx">FlowDocumentPageViewer</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx">RichTextBox</a>, or a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentscrollviewer.aspx">FlowDocumentScrollViewer</a> ... there&#8217;s lots more information about those <a href="http://msdn.microsoft.com/en-us/library/aa970909.aspx">on msdn</a>.</p>

	<h2>Time for some artwork</h2>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-04.png" class="float-right-block" alt="" width="179" height="171" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Ellipse <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">60</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">80</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;20,10,60,20&quot;</span> <span style="color: #000066;">-Fill</span> Black <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>In <span class="caps">WPF</span>, everything always starts out white, and you must position things based on the container. You can see that the Margin can be specified as a single value as in the previous example, or as separate values for Left, Top, Right, Bottom.  Oddly, to satisfy PowerShell&#8217;s type-casting, you have to quote them so they&#8217;re a single comma-separated string, instead of four separate values.</p>

	<h2>Some more advanced drawing</h2>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-05.png" class="float-right-block" alt="" width="155" height="160" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Canvas <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Children</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;Rectangle <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;10,10,0,0&quot;</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">45</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">45</span> <span style="color: #000066;">-Stroke</span> <span style="color: #009900;">&quot;#689945&quot;</span> <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Fill</span> <span style="color: #009900;">&quot;#336699&quot;</span><br />
&nbsp; &nbsp;Polygon <span style="color: #000066;">-Stroke</span> Pink <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Fill</span> DarkRed <span style="color: #000066;">-Points</span> <span style="color: #009900;">&quot;10,60&quot;</span>, <span style="color: #009900;">&quot;50,60&quot;</span>, <span style="color: #009900;">&quot;50,50&quot;</span>, <span style="color: #009900;">&quot;65,65&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;50,80&quot;</span>, <span style="color: #009900;">&quot;50,70&quot;</span>, <span style="color: #009900;">&quot;10,70&quot;</span>, <span style="color: #009900;">&quot;10,60&quot;</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>When you want to start getting clever and overlapping things, you need to use a Canvas container. The Canvas can contain multiple items which are all absolutely positioned, but unlike most other containers, it doesn&#8217;t automatically expand to contain it&#8217;s children, so you typically have to set it&#8217;s size.</p>

	<p>We also have to set the Stroke and Fill.  These are the two colors that make up every object, and again, if we don&#8217;t set them, they default to white. Note that you can use named colors, or you can specify a hex value using &#8220;#RRGGBB&#8221; or &#8220;#AARRGGBB&#8221; to set the alpha channel. The StrokeThickness controls the line thickness.  </p>

	<p>One other thing to notice is that we positioned the Rectangle by using the <code>Margin</code>, but we positioned the arrow, which we built using a Polygon, based purely on the x,y coordinates of the points.  The available shapes are Ellipse, Line, Path, Polygon, Polyline, and Rectangle.  You can, of course, make nearly any shape you want with the Polygon.</p>

	<p>There are other more advanced shapes available in external libraries, and we can even do 3D, use gradient or image fills&#8230;</p>

	<h3>We can even get images straight off the web</h3>

	<div class="posh code posh" style="font-family:monospace;"><br />
&nbsp; &nbsp;Image <span style="color: #000066;">-Source</span> http:<span style="color: #66cc66;">//</span>huddledmasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>images<span style="color: #66cc66;">/</span>PowerBoots<span style="color: #66cc66;">/</span>IMG_3298.<span style="color: #003366;">jpg</span> <span style="color: #000066;">-MaxWidth</span> <span style="color: #cc66cc;">400</span> <span style="color: #000066;">-AsJob</span><br />
&nbsp;</div>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-06.png" class="float-right-block" alt="" width="437" height="729" /></p>

	<p><span class="caps">WPF</span> loads the image on a background thread, and caches it in memory, so the window will show up and be responsive while you&#8217;re waiting for the image, and because we&#8217;ve specified <code>-AsJob</code>, you can actually continue using PowerShell while the image loads. Note: it will load much faster the second time you run that script.  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<h3>Events</h3>

	<p>If you were paying attention to the typography example, you&#8217;ll have noticed that we introduced event handling without making a big fuss about it. Event handlers in PowerBoots are specified in much the same way that Properties are, except that their parameter names always start with &#8220;On_&#8221; and they take a script block.  The Hyperlink element in a <span class="caps">WPF</span> window doesn&#8217;t automatically open a browser (because you can use it to change &#8220;pages&#8221; in a <span class="caps">WPF</span> application), so to make simple web links work, you have to handle the &#8220;RequestNavigate&#8221; event as shown above.</p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-08.png" class="float-right-block" alt="" width="259" height="86" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
WrapPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">'Click Counter'</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Button <span style="color: #009900;">&quot;Push Me&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #333;">&#123;</span>Click Counter<span style="color: #333;">&#125;</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#123;</span>Click Counter<span style="color: #333;">&#125;</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$CountLabel</span>.<span style="color: #003366;">Content</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;You clicked the button $(${Click Counter}.Tag) times!&quot;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Nothing pushed so far&quot;</span> <span style="color: #000066;">-Name</span> CountLabel<br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>In order to update your user interface when an event triggers, you&#8217;ll need to have variables that point at the control(s) you want to affect.  In ShowUI event handlers, you get a <code>$this</code> variable which points at the object that caused the event, a <code>$_</code> variable which is the event arguments, and a <code>$window</code> variable for the top-level window &#8230; you also get variables for each named control in your window.  </p>

	<p><span class="caps">WPF</span> Elements all have a <code>Tag</code> property which can be used to store any object, so as you can see in the previous example, you can use that to keep track of things &#8230; but more importantly, the value of the <code>Tag</code> on the top-level control will be output. In other words, when the window is closed, this example actually outputs to the PowerShell pipeline how many times you clicked the button.</p>

	<h3>Working with User Input</h3>

	<p>There are helper functions in ShowUI for working with that output value: <code>Set-UIValue</code> and <code>Get-UIValue</code>, and Get-UIValue is particularly helpful because if the control it&#8217;s called on has no value, it collects the values of all the child controls, so you can use it to output a whole form at once:</p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-09.png" class="float-right-block" alt="" width="289" height="260" /></p>

	<p>I&#8217;ve made this example more complicated than it needed to be to demonstrate some best practices.  We could have made it much simpler by using a UniformGrid control instead of a GridControl, thus avoiding needing to set the -Row and -Column special parameters, but I wanted to show those to you anyway, and the form looks a lot better when the two columns can have different sizes:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Grid <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">'Your Information'</span> <span style="color: #000066;">-Columns</span> Auto,<span style="color: #66cc66;">*</span> <span style="color: #000066;">-Rows</span> <span style="color: #cc66cc;">7</span> <span style="color: #000066;">-MinHeight</span> <span style="color: #cc66cc;">200</span> <span style="color: #000066;">-MinWidth</span> <span style="color: #cc66cc;">250</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;First Name&quot;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> First <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Last Name&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">1</span> <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> Last <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Address&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> Address <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;City&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">3</span> <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> City <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">3</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;State&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">4</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> State <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">4</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Zip&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">5</span> <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> Zip <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Button</span></span> <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-IsDefault</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ParentControl</span></span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">UIValue</span></span> <span style="color: #000066;">-PassThru</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Close-<span style="font-style: normal;">Control</span></span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">6</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$First</span>.<span style="color: #003366;">Focus</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>You&#8217;ll notice how easily I specified the width of the columns, but the <span class="caps">COUNT</span> of the rows in the Grid panel. If you provide an array of values, they&#8217;re converted to GridLengths, but if you provide just one, it&#8217;s treated as the count. You can make grid columnds and rows AUTOmatically size to their contents, and you can make them * width to make them take up any extra space.  You can even split the extra space by setting * on more than one column (and specify the proportion by using numbers, like: 1*, 2*).</p>

	<p>I should also point out that if you use MinHeight and MinWidth instead of the Height and Width values, your controls will be able to size up to fill space when the window is resized! Try that script with Width and Height instead and resize the window to see the difference.</p>

	<p>We used the Set-UIValue shortcut in this form, which brings up another point: when creating data forms, you only need to name the controls you want output from. Then you&#8217;ll be able to just call Set-UIValue on the parent to collect all the values from the controls and output them as a hashtable!</p>

	<p>Finally, remember your defaults: set the focus to something so the user doesn&#8217;t <span class="caps">HAVE</span> to click to get started, and set a button to -IsDefault with a On_Click handler so that when the user hits enter they can submit the form.</p>

	<h3>Further directions</h3>

	<p>There&#8217;s a lot more possible with ShowUI: we can use gradients for colors, create data templates and styles, and even make chromeless windows, but you have the basics for getting started already.</p>

	<p>This first release of ShowUI is still missing some features from both <span class="caps">WPK</span> and PowerBoots, but we&#8217;ll get to them as we progress.  For now we&#8217;d like to invite you to come <a href="http://showui.codeplex.com/releases/">download the latest release</a>, and <a href="http://showui.codeplex.com/workitem/list/basic">write up or vote up the features</a> that you want the most in the next version.  Keep an eye on the release page and on the discussions and we&#8217;ll be cranking out new releases on a monthly basis for now.</p>

	<p>I hope you&#8217;ve enjoyed this tour through ShowUI, and will be able to start applying it soon for fun and profit.</p>

	<p id="fn1" class="footnote"><sup>1</sup> The -<span class="caps">STA</span> switch is necessary because the .Net framework requires the <acronym title="Single Threaded Apartment">STA</acronym> threading model in order to do graphical user interfaces. PowerShell <span class="caps">ISE</span> doesn&#8217;t require it because unlike the PowerShell.exe command console (which is a Win32 native &#8220;console&#8221; application that hosts the .Net framework), PowerShell <span class="caps">ISE</span> is a .Net framework &#8220;graphical&#8221; application, and is therefore running in the <span class="caps">STA</span> model already.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/showui-tutorial-walkthrough/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PowerBoots (Next Generation UI) WPF for PowerShell slides</title>
		<link>http://joelbennett.net/powerboots-presentation-2011-02/</link>
		<comments>http://joelbennett.net/powerboots-presentation-2011-02/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 04:29:41 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[UserInterface]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1657</guid>
		<description><![CDATA[I gave a LiveMeeting presentation on PowerBoots last week, and these are the slides. I&#8217;m sorry to say that the recording did not work out, so you&#8217;ll have to wait for the next time I present it to see the demos. Luckily, the demo scripts I used are mostly the samples which are available in [...]]]></description>
			<content:encoded><![CDATA[	<p>I gave a LiveMeeting presentation on PowerBoots last week, and these are the slides. I&#8217;m sorry to say that the recording did not work out, so you&#8217;ll have to wait for the next time I present it to see the demos. Luckily, the demo scripts I used are mostly the samples which <em>are</em> available in the PowerBoots distributions.</p>

	<ul>
		<li><a href="/downloads/PowerBoots/PowerBoots-2011.pptx" title="2.13Mb">PowerPoint Deck</a></li>
	</ul>
	<ul>
		<li><a href="/downloads/PowerBoots/PowerBoots-2011.pdf" title="973Kb"><span class="caps">PDF</span> Deck with notes</a></li>
	</ul>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powerboots-presentation-2011-02/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerBoots 0.3 – The Faster Edition</title>
		<link>http://joelbennett.net/powerboots-0-3-the-faster-edition/</link>
		<comments>http://joelbennett.net/powerboots-0-3-the-faster-edition/#comments</comments>
		<pubDate>Sat, 19 Jun 2010 05:40:53 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Teaser]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1496</guid>
		<description><![CDATA[I&#8217;ve been working on a ton of new functionality for this next release of PowerBoots &#8230; and now that I&#8217;m getting close to ready to release, I thought it was time to work on the performance! Here&#8217;s a completely trivial example: WPK: Command : $boxes = 1..100 &#124; % { New-TextBox -Text Foo -FontFamily Consolas [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve been working on a ton of new functionality for this next release of PowerBoots &#8230; and now that I&#8217;m getting close to ready to release, I thought it was time to work on the performance! Here&#8217;s a completely trivial example:</p>

	<h3>WPK:</h3>

	<p>Command   : $boxes = 1..100 | % { New-TextBox -Text Foo -FontFamily Consolas -On_GotFocus { $_ } }<br />
Duration  : 5.25153s</p>

	<h3>Current PowerBoots 0.2:</h3>

	<p>Command   : $boxes = 1..100 | % { TextBox -Text Foo -FontFamily Consolas -On_GotFocus { $_ } }<br />
<strong>Average   : 9.40994s</strong></p>

	<p>You can see why I thought I should probably work on improving that. It was bad.</p>

	<h3>PowerBoots 0.3 yesterday:</h3>

	<p>Command   : $boxes = 1..100 | % { TextBox -Text Foo -FontFamily Consolas -On_GotFocus { $_ } }<br />
Duration  : 7.43874s</p>

	<h3>PowerBoots 0.3 tonight:</h3>

	<p>Command   : $boxes = 1..100 | % { TextBox -Text Foo -FontFamily Consolas -On_GotFocus { $_ } }<br />
<strong>Duration  : 0.34103s</strong></p>

	<p>Now, if you&#8217;re like me, you&#8217;re probably thinking I screwed up <em>something</em> and that TextBox isn&#8217;t actually getting created right &#8230;</p>

	<p><img src="http://joelbennett.net/images/PowerBoots/Boots-Performance-Boost.png" alt="" width="902" height="573" /></p>

	<p>Postscript: ran another test. A little more useful. I have 248 fonts on my system.  I have an &#8220;Samples&#8221; script which shows them all:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #333;">&#123;</span> ScrollViewer <span style="color: #333;">&#123;</span> TextBlock <span style="color: #009900;">&quot;Loading Fonts...&quot;</span> <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">62</span> <span style="color: #000066;">-FontFamily</span> SegoeUI <span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span> <span style="color: #000066;">-Async</span> <span style="color: #000066;">-Passthru</span> <span style="color: #66cc66;">|</span> <br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-Script</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$This</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">Content</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">ForEach</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$font</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Media</span>.<span style="color: #003366;">Fonts</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">SystemFontFamilies</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBlock <span style="color: #000066;">-FontFamily</span> <span style="color: #660033; font-weight: bold;">$font</span>.<span style="color: #003366;">Source</span> <span style="color: #000066;">-Text</span> <span style="color: #009900;">&quot;The Quick Brown Fox Jumps over the Lazy Dog&quot;</span> <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">18</span>; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #660033; font-weight: bold;">$font</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> StackPanel<br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<h3>Old PowerBoots:</h3>

	<p>Duration  : 19.39894s</p>

	<h3>New PowerBoots:</h3>

	<p>Duration  : 3.53100s</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powerboots-0-3-the-faster-edition/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Creating WPF UIs for PowerShell with PowerBoots and Visual Studio WPF Designer</title>
		<link>http://joelbennett.net/creating-wpf-uis-for-powershell-with-powerboots-and-visual-studio-wpf-designer/</link>
		<comments>http://joelbennett.net/creating-wpf-uis-for-powershell-with-powerboots-and-visual-studio-wpf-designer/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 03:09:42 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1433</guid>
		<description><![CDATA[I&#8217;ve had several people ask me how PowerBoots compares to PrimalForms, or ask for a visual designer for PowerBoots. I usually answer something along the lines of the fact that Microsoft has already created a very good WPF/XAML designer in Visual Studio (including the free Express editions), particularly in 2010, so I don&#8217;t see why [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve had several people ask me how <a href="http://boots.codeplex.com/">PowerBoots</a> compares to PrimalForms, or ask for a visual designer for PowerBoots. I usually answer something along the lines of the fact that Microsoft has already created a very good WPF/<span class="caps">XAML</span> designer in Visual Studio (including the free Express editions), particularly in 2010, so I don&#8217;t see why I should duplicate their efforts.  However, up until now I haven&#8217;t written or published any sort of walk through about how to make that work &#8230; </p>

	<p>So crank up your <a href="http://msdn.com/express/">Visual Studio Express Edition</a> and make yourself some user interfaces!</p>

	<p>Basically, you just create a <span class="caps">WPF</span> project in C# or VB.Net or whatever &#8230; I chose to name my project &#8220;XamlForBoots&#8221; &#8212; your project will start off with an empty MainWindow.xaml file which will look something like this:</p>

	<a href="/wordpress/wp-content/uploads/2010/03/NewMainWindow.xaml_.png"><img src="/wordpress/wp-content/uploads/2010/03/NewMainWindow.xaml_-300x199.png" alt="This is what the Main Window looks like at first" title="NewMainWindow.xaml" width="300" height="199" class="size-medium wp-image-1434" /></a>

	<p>After you drag a few more controls onto the form, and you&#8217;ve created a complete user interface, you should delete the x:Class attribute.  You need to make sure you know the <em>name</em> of the controls you want to interact with, so you might end up with something like this (I used the property pane, which you can&#8217;t see in this shot, to name each textbox and the button):</p>

	<a href="/wordpress/wp-content/uploads/2010/04/FinishedMainWindow.xaml_.png"><img src="/wordpress/wp-content/uploads/2010/04/FinishedMainWindow.xaml_-300x212.png" alt="Once you&#039;ve dragged some controls onto it" title="FinishedMainWindow.xaml" width="300" height="212" class="size-medium wp-image-1439" /></a>

	<p>At this point, we&#8217;re ready to drop into PowerShell and write some script. Now &#8230; there is one catch here.  The first script I&#8217;m going to show you here is for <strong>PowerBoots 0.3</strong> (which will be the first release candidate for a gold 1.0 release, and will be out soon&#8482;). However, I&#8217;ll post below some code to make it work on the current release, but it requires an external function.</p>

	<p>So, in the next release, you can just do something like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-FileTemplate</span> <span style="color: #660033; font-weight: bold;">$pwd</span>\MainWindow.<span style="color: #003366;">xaml</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">BootsEvent</span></span> <span style="color: #000066;">-InputObject</span> <span style="color: #660033; font-weight: bold;">$Calculate</span> <span style="color: #000066;">-EventName</span> Click <span style="color: #000066;">-Action</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Total</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">'${0:n2}'</span> <span style="color: #000066;">-f</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Miles</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Mpg</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Cost</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>The key thing you&#8217;re supposed to notice here is that the named controls in the <span class="caps">XAML</span> are automatically surfaced as variables in the event handlers, and all you have to do is write your logic and hook it up to the controls. We provide a <code>Register-BootsEvent</code> cmdlet which is like Register-ObjectEvent except that it executes the event handlers on the UI thread (so they can do things to the UI) instead of in a new runspace, but basically it&#8217;s like calling <code>Add_Click</code>. Of course, you <strong>can</strong> use Register-ObjectEvent if you just want to spin off PowerShell tasks that don&#8217;t read/write the UI.</p>

	<h3>Backwards compatibility</h3>

	<p>To get this to work in the current release, you need a function <code>Export-NamedControl</code> to create variables for each of those named controls as variables. Once you&#8217;ve defined that function, you can write code very much like I did before, but you have to <em>call Export-NamedControl yourself</em>, and you won&#8217;t have that Register-BootsEvent cmdlet. You don&#8217;t really need it for this anyway, so that&#8217;s not a big deal, at least in this case. Here&#8217;s the function. and the actual code to create the window.  You can just paste this into a script file:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> global:<span style="color: #0066cc; font-style: italic;">Export-<span style="font-style: normal;">NamedControl</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>, Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>, Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Root</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$BootsWindow</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">process</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$Root</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$control</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$BootsWindow</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$control</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$control</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$control</span> <span style="color: #66cc66;">|</span> ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Element</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Element</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">return</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;This $($Element.GetType().Name) is $Element&quot;</span><br />
&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Name</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;Defining $($Element.Name) = $Element&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Variable</span></span> <span style="color: #009900;">&quot;$($Element.Name)&quot;</span> <span style="color: #660033; font-weight: bold;">$Element</span> <span style="color: #000066;">-Scope</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## Return all the child controls ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Children</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Child</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Content</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Items</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Inlines</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Blocks</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">####################################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Create the window and hook the click. Make sure to use full paths</span><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #333;">&#123;</span><span style="color: #333;">&#125;</span> <span style="color: #000066;">-FileTemplate</span> <span style="color: #660033; font-weight: bold;">$pwd</span>\MainWindow.<span style="color: #003366;">xaml</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Export-<span style="font-style: normal;">NamedControl</span></span> <span style="color: #000066;">-Root</span> <span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Calculate</span>.<span style="color: #003366;">Add_Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Total</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">'${0:n2}'</span> <span style="color: #000066;">-f</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Miles</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Mpg</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Cost</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>And in order to try <strong>my</strong> demo, you&#8217;re going to need that MainWindow.xaml file:</p>

	<div class="xml code xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;Trip Cost&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;28&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;28&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;28&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;28&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;110&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;*&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Label</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Miles&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;23&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;miles&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Label</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Miles per Gallon&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;23&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;mpg&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Label</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Cost per Gallon&quot;</span> &nbsp;<span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;23&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;cost&quot;</span> &nbsp;<span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;calculate&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;_Calculate&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;23&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;total&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/creating-wpf-uis-for-powershell-with-powerboots-and-visual-studio-wpf-designer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Format-PoshTable &#8211; A Video</title>
		<link>http://joelbennett.net/format-poshtable-a-video/</link>
		<comments>http://joelbennett.net/format-poshtable-a-video/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 22:45:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1416</guid>
		<description><![CDATA[Format-PoshTable (PowerBoots + DataGrid from WPFToolkit + PoshConsole) on ScreenCast or Vimeo (and PoshCode). If you can&#8217;t view that (it requires Windows Media Player), you can check it out through Vimeo below. I apologize for the size and quality of that, I didn&#8217;t read the HD instructions ahead of time. Skip to the end for [...]]]></description>
			<content:encoded><![CDATA[	<p><object name="Video" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject"  width="800" height="665"> <param name="url" value="http://content.screencast.com/users/Jaykul/folders/PowerShell/media/7c2b195b-a944-4a44-94fc-7a2ef5601e0c/Format-PoshTable.wmv"></param> <param name="AutoStart" value="0"></param> <param name="ShowControls" value="true"></param> <param name="uiMode" value="full"></param> <param name="playCount" value="1"></param> <param name="CurrentPosition" value="0"></param> <embed name="Video" type="application/x-mplayer2" src="http://content.screencast.com/users/Jaykul/folders/PowerShell/media/7c2b195b-a944-4a44-94fc-7a2ef5601e0c/Format-PoshTable.wmv" autoStart="0" showcontrols="1" uimode="full" playcount="1" currentposition="0" width="800" height="665"></embed></object></p>

	<p>Format-PoshTable (PowerBoots + <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.autogeneratecolumns(VS.100).aspx">DataGrid</a> from <a href="http://wpf.CodePlex.com">WPFToolkit</a> + PoshConsole) on  <a href="http://www.screencast.com/t/ODhhNWE0ZDUt">ScreenCast</a> or <a href="http://vimeo.com/10141844">Vimeo</a> (and <a href="http://poshcode.org/1699">PoshCode</a>).</p>

	<p>If you can&#8217;t view that (it requires Windows Media Player), you can check it out through Vimeo below.  I apologize for the size and quality of that, I didn&#8217;t read the HD instructions ahead of time. Skip to the end for the code  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<p><object width="800" height="600"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=10141844&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=10141844&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="600" height="450"></embed></object><p>A simple demo of some of the capabilities of PowerBoots and PoshConsole in the form of a grid-output function...</p></p>

	<p><script type="text/javascript" src="http://PoshCode.org/embed/1699"></script></p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/format-poshtable-a-video/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
<enclosure url="http://content.screencast.com/users/Jaykul/folders/PowerShell/media/7c2b195b-a944-4a44-94fc-7a2ef5601e0c/Format-PoshTable.wmv" length="8616801" type="video/x-ms-wmv" />
		</item>
		<item>
		<title>WPF in PowerShell: PowerBoots 0.2</title>
		<link>http://joelbennett.net/wpf-in-powershell-powerboots-02/</link>
		<comments>http://joelbennett.net/wpf-in-powershell-powerboots-02/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 04:14:54 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1183</guid>
		<description><![CDATA[I&#8217;ve finally given over trying to improve PowerBoots for this iteration of development, and in between setting up the ScriptingGames.PoshCode site and releasing the PoshCode software on the main PoshCode.org site (it&#8217;s coming, I promise), I decided to take a few minutes and release this lates PowerBoots, and it&#8217;s a ground-breaking release, if I do [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve finally given over trying to improve PowerBoots for this iteration of development, and in between setting up the <a href="http://scriptinggames.poshcode.org">ScriptingGames.PoshCode</a> site and releasing the PoshCode software on the main PoshCode.org site (it&#8217;s coming, I promise), I decided to take a few minutes and release this lates PowerBoots, and it&#8217;s a <a href="http://powerboots.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=28954">ground-breaking release</a>, if I do say so myself!</p>

	<h3>PowerShell 1.0 Compatibility</h3>

	<p>There are three huge changes in this release, actually. But the biggest one is that <em>most</em> of the functionality works in the released version of PowerShell 1.0!  There are still a few glitches and bugs when it&#8217;s running in 1.0, but I&#8217;ve decided it&#8217;s past time I just release it and let you start using it!  If you use it on 1.0, please let me know if you run into anything weird, or problems you can&#8217;t figure out workarounds for.</p>

	<h3>Cached Script Functions!</h3>

	<p>The second biggest change is that all of the functions are now cached. The first time it&#8217;s loaded, PowerBoots actually writes out a few hundred scripts files into a &#8220;Functions&#8221; subfolder, and from then on, they&#8217;re only loaded when they&#8217;re used &#8212; saving you <em>a lot</em> of time at startup, and <em>a lot</em> of memory.  Additionally, when you load in additional controls, such as the <a href="http://www.visifire.com/">Visifire <span class="caps">WPF</span> chart controls</a>, those functions are cached permanently too, so you never even have to manually load the assembly again!</p>

	<h3>Dependency Properties</h3>

	<p>A few people have asked over the last few months about how to <a href="http://huddledmasses.org/powerboots-and-attached-properties/">set attached properties and dependency properties in <span class="caps">WPF</span> from PowerShell</a>, so in this release I&#8217;ve included full support for these in a way that will, I hope, simply work so well it will boggle your mind  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=':)' class='wp-smiley' />  All you have to do to tell a control, for instance, to be in the 3rd column of a grid is to just pass <code>Button &#34;Click Me&#34; -&#34;Grid:Column&#34; 2</code> &#8230; it&#8217;s <span class="caps">THAT</span> simple. In fact, for properties that have unique names, you don&#8217;t even have to specify the type, just the name, like <code>-Row 3</code> to specify the grid row.</p>

	<h3>Regression and Backwards Compatibility </h3>

	<p>Of course, the downside of these changes is that some of the syntax has changed a little bit. All scripts and event handlers (ie: click handlers for <span class="caps">WPF</span> Buttons) are run from the <strong>dll</strong> module scope in PowerShell 2 (the global scope in PowerShell 1), which means that they can&#8217;t access &#8220;script&#8221; scope variables from your scripts (which ends up meaning you have to declare a lot of things global that you could leave in script scope before).  Just to be clear on the quality of this release: once we have ironed out the minor problems we&#8217;re having with PowerShell 1.0 in a way that I&#8217;m comfortable with, I expect to push the version number up and release an 1.0 Release Candidate, barring any major issues.</p>

	<p>In order to help with the transition, I&#8217;ve included a Samples.ps1 script which has 28 sample scripts in it (mostly from the original <a href="http://huddledmasses.org/powerboots-tutorial-walkthrough/">Tutorial Walk-through</a>, which I will update soon).  I&#8217;ve updated each of them to work with this release, in both PowerShell 1 and PowerShell 2 (which means they&#8217;re almost all written in PowerShell 1 syntax), so you can use them as good examples of how to get things done!</p>

	<p>Please use the Discussions Forum and Issue Tracker on <a href="http://boots.CodePlex.org">the CodePlex site</a> to post any problems you have, and I&#8217;ll try to write up a few more posts describing changes and differences in the next couple of weeks.</p>

<h6 class="zemanta-related-title">Related articles by Zemanta</h6><ul class="zemanta-article-ul"><li class="zemanta-article-ul-li"><a href="http://huddledmasses.org/powerboots-loading-xaml-windows-in-powershell-10-or-20/">PowerBoots: Loading <span class="caps">XAML</span> Windows in PowerShell 1.0 or 2.0</a> (huddledmasses.org)</li><li class="zemanta-article-ul-li"><a href="http://huddledmasses.org/powerboots-now-multi-threaded/">PowerBoots: PowerShell GUIs are now multi-threading</a> (huddledmasses.org)</li><li class="zemanta-article-ul-li"><a href="http://huddledmasses.org/pingmonitor-interactive-wpf-uis-from-powershell-10-or-20/">PingMonitor: interactive <span class="caps">WPF</span> UIs from PowerShell (1.0 or 2.0)</a> (huddledmasses.org)</li></ul>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/3373c251-af5f-4875-a3fa-7f0c2581d019/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=3373c251-af5f-4875-a3fa-7f0c2581d019" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/wpf-in-powershell-powerboots-02/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PowerBoots: The tutorial walkthrough</title>
		<link>http://joelbennett.net/powerboots-tutorial-walkthrough/</link>
		<comments>http://joelbennett.net/powerboots-tutorial-walkthrough/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 16:21:13 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerTips]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WalkThrough]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=963</guid>
		<description><![CDATA[Updated to PowerBoots 0.1 An introduction to PowerBoots Please excuse me if I start by just copying the basic ideas of the Shoes Tutorial, but I figured that since PowerBoots is inspired by Shoes, that was as good a place as any to start. PowerBoots (or just &#8220;Boots&#8221;) is a PowerShell 2.0 module with functions [...]]]></description>
			<content:encoded><![CDATA[	<h3> <img src='http://joelbennett.net/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  Updated to PowerBoots 0.1</h3>

	<h2>An introduction to PowerBoots</h2>

	<p>Please excuse me if I start by just copying the basic ideas of the <a href="http://shoooes.net/tutorial/">Shoes Tutorial</a>, but I figured that since PowerBoots is inspired by Shoes, that was as good a place as any to start.  PowerBoots (or just &#8220;Boots&#8221;) is a PowerShell 2.0 module with functions for writing Windows Presentation Framework (<span class="caps">WPF</span>) applications in the PowerShell scripting language.  You should get <a href="http://boots.CodePlex.com">the latest version of PowerBoots</a> before continuing, and install it by putting the &#8220;PowerBoots&#8221; folder in one of your &#8220;Modules&#8221; folders (list them by typing <code>$Env:PSMODULEPATH</code> in PowerShell v2). </p>

	<p><del>Don&#8217;t forget to start PowerShell.exe with the <span class="caps">STA</span> parameter</del> (This is no longer required in PowerBoots 0.1).</p>

	<p>Did I hear someone ask <strong>what <em>is</em> WPF?</strong> It was introduced as part of .Net 3.0 (and vastly improved in .Net 3.5), so you can expect to find it preinstalled on computers from Vista on, and of course you can download and install it on XP if it&#8217;s not already installed.  The only thing you really need to know about <span class="caps">WPF</span> for the purposes of this tutorial is that it is <strong>the</strong> new <span class="caps">GUI</span> toolkit for .Net, and that it is container based &#8212; you put elements into other elements to control the layout, rather like <span class="caps">HTML</span> and Java Swing&#8230; you can <strong>pick up the rest as we go along</strong>.</p>

	<h2>A simple Boots program</h2>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots1.png" class="float-right-block" alt="" width="144" height="86" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-SizeToContent</span> WidthAndHeight <span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;Button <span style="color: #000066;">-Content</span> <span style="color: #009900;">&quot;Push Me&quot;</span> <br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>I&#8217;ve recently <strong>changed</strong> this first example to pass a script block to the content instead of using parentheses, because although PowerBoots supports running in an <span class="caps">MTA</span> PowerShell host, the only UI-creation command that is MTA-safe is the New-BootsWindow command. All of the &#8220;new&#8221; functions must then be inside of a scriptblock which is passed to the New-BootsWindow cmdlet and executed on that <span class="caps">STA</span> thread. You can use parenthesis ( and ) as a container instead, but that requires the host to be in <span class="caps">STA</span> threading mode (run: PowerShell -<span class="caps">STA</span>) so the controls can be created:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-SizeToContent</span> WidthAndHeight <span style="color: #000066;">-Content</span> <span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp;Button <span style="color: #000066;">-Content</span> <span style="color: #009900;">&quot;Push Me&quot;</span><br />
<span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>This first example is a bit uglier than the Shoes syntax, so lets see if we can&#8217;t clean it up some. The <code>-Content</code> parameter is positional, so the first non-named argument you pass will be used for that. The same is true for the -Children parameter of panels, and in fact, each of the other similar parameters: Items, Blocks, and Inlines.</p>

	<p>We have used a function <code>New-BootsWindow</code> which has an alias <code>Boots</code>. Boots takes all the same parameters as the <code>Window</code> function mentioned previously, but it uses slightly more useful defaults, and has a few other major benefits as well, the first of which is that it automatically &#8220;shows&#8221; the window, and the second is that it supports an <code>-Async</code> parameter which allows the window to come out in a new thread so that you can continue using PowerShell while the window remains alive and responsive.  There is one catch: New-BootsWindow <em>cannot</em> take it&#8217;s content on the pipeline (the <em>old function</em>, now renamed &#8220;Out-BootsWindow&#8221; can take pipeline content, but is a script function, and requires -<span class="caps">STA</span> mode) &#8212; you have to specify it as a ScriptBlock. So now that we know this, we can rewrite our first example like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> Button <span style="color: #009900;">&quot;Push Me&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Just for the record, the simplest Boots program would just be a simple popup dialog to put some text in a Window, like:  <code>Boots { $msg }</code> &#8230;</p>

	<h2>We can put controls in a stack</h2>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots2.png" class="float-right-block" alt="" width="156" height="128" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;StackPanel <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A bed of clams&quot;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A gulp of swallows&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>StackPanels are awesome. So are WrapPanels.  Try that code with a WrapPanel instead of a StackPanel and see what the difference is.  This brings up another point: those positional parameters we mentioned earlier: Content, Children, Items, Blocks, and Inlines, are also set to accept the value from the pipeline.  Not only that, but they are intelligent about whether or not the content model accepts multiple items! So we can actually rewrite that script like this, and get the same results:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span>, <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span>, <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #66cc66;">|</span> Button <span style="color: #66cc66;">|</span> StackPanel <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Now we&#8217;re really onto something! <span id="more-963"></span> For most of the rest of these examples, I&#8217;m going to stick with the former syntax, because with the indenting and parenthesis, it&#8217;s much easier for you to follow, especially if you&#8217;re not already familiar with <span class="caps">WPF</span>.  For instance, check out what this does:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span>, <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span>, <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #66cc66;">|</span> StackPanel <span style="color: #66cc66;">|</span> Button <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<h2>Ok, lets see some formatting</h2>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots3.png" class="float-right-block" alt="" width="174" height="152" /></p>

	<p>To scoot the buttons out from the edge we can use margins or padding: margins go on the outside of containers, padding goes on the inside.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Background</span> Pink $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A gulp of swallows&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Or, on one line:</span><br />
<br />
Boots <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span>, <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span>, <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #66cc66;">|</span><br />
&nbsp; &nbsp;Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">|</span> StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Background</span> Pink <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>So you see, the pink background is on the StackPanel, which has a (white) margin around it.  If you wanted the whole background of the window to be pink, you would need to set the background of the Window instead of the StackPanel.</p>

	<h2>Time for some artwork</h2>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots4.png" class="float-right-block" alt="" width="176" height="164" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> Ellipse <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">60</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">80</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;20,10,60,20&quot;</span> <span style="color: #000066;">-Fill</span> Black <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>In Boots, everything always starts out white, and you must position things based on the container. You can see that the Margin can be specified as a single value as in the previous example, or as separate values for Left, Top, Right, Bottom.  Oddly, to satisfy PowerShell&#8217;s type-casting, you have to quote them so they&#8217;re a single comma-separated string, instead of four separate values.</p>

	<h2>Some more advanced drawing</h2>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots5.png" class="float-right-block" alt="" width="148" height="158" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
Canvas <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Children</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;Rectangle <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;10,10,0,0&quot;</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">45</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">45</span> <span style="color: #000066;">-Stroke</span> Purple <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Fill</span> Red<br />
&nbsp; &nbsp;Polygon <span style="color: #000066;">-Stroke</span> Pink <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Fill</span> DarkRed <span style="color: #000066;">-Points</span> <span style="color: #009900;">&quot;10,60&quot;</span>, <span style="color: #009900;">&quot;50,60&quot;</span>, <span style="color: #009900;">&quot;50,50&quot;</span>, <span style="color: #009900;">&quot;65,65&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;50,80&quot;</span>, <span style="color: #009900;">&quot;50,70&quot;</span>, <span style="color: #009900;">&quot;10,70&quot;</span>, <span style="color: #009900;">&quot;10,60&quot;</span> <br />
<span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>We use a Canvas for this because it can contain multiple items which are all absolutely positioned.  Unlike other containers, it doesn&#8217;t automatically expand to contain it&#8217;s children, so you typically have to set it&#8217;s size.</p>

	<p>We also have to set the Stroke and Fill.  These are the two colors that make up every object, if we don&#8217;t set them, they default to white. The StrokeThickness controls the line thickness.  Notice that we positioned the Rectangle by using the <code>Margin</code>, and positioned the arrow, which we built using a Polygon, based purely on the x,y coordinates of the points.  The available shapes are Ellipse, Line, Path, Polygon, Polyline, and Rectangle.  You can, of course, make any shape you want to with the Polygon.</p>

	<p>There are other more advanced shapes available in external libraries, and we can even do 3D, use gradient or image fills&#8230;</p>

	<h3>We can even get images straight off the web</h3>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;Image <span style="color: #000066;">-Source</span> http:<span style="color: #66cc66;">//</span>huddledmasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>images<span style="color: #66cc66;">/</span>PowerBoots<span style="color: #66cc66;">/</span>IMG_3298.<span style="color: #003366;">jpg</span> <span style="color: #000066;">-MaxWidth</span> <span style="color: #cc66cc;">400</span> <span style="color: #66cc66;">|</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Title</span> <span style="color: #009900;">&quot;Now those are some powerful boots!&quot;</span> <span style="color: #000066;">-Async</span><br />
&nbsp;</div>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots6.png" class="float-right-block" alt="" width="436" height="730" /></p>

	<p>Boots loads the image on a background thread, and caches it in memory, so the window will show up and be responsive while you&#8217;re waiting for the image, and because we&#8217;ve specified <code>-Async</code>, you can actually continue using PowerShell while the image loads. Note: it will load much faster the second time you run that script.  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

<h3 style="clear: both;">Typography</h3>

	<p>PowerBoots doesn&#8217;t try to create a full set of typography-specific top-level elements the way Shoes does, because we are based on <span class="caps">WPF</span>, which has a far more powerful typography system available than any we&#8217;ve ever used.  So instead of having a bunch of named elements like banner, and title, and caption, and para and whatnot, we have controls based on how much text you want to put in them, and how much formatting you want to apply: Label is simplest, TextBlock supports limited text formattings, and FlowDocument supports full rich content. And of course, Hyperlink supports clicking  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots7.png" class="float-right-block" alt="" width="466" height="202" /></p>

	<p>For the typography elements, the content model changes a bit.  There are basically two types: Inline and Block elements. The <a href="http://msdn.microsoft.com/en-us/library/bb613554.aspx">TextBlock Content Model</a> is similar to that of a FlowDocument, it is actually a type-restricted &#8220;Items&#8221; container.  Instead of being able to have <em>anything</em> as content, it can only contain <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.inline.aspx">Inline</a> flow content elements such as <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.anchoredblock.aspx">AnchoredBlock</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.bold.aspx">Bold</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.hyperlink.aspx">Hyperlink</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.inlineuicontainer.aspx">InlineUIContainer</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.italic.aspx">Italic</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.linebreak.aspx">LineBreak</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.run.aspx">Run</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.span.aspx">Span</a>, and <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.underline.aspx">Underline</a>, and it will create a run automatically if you just put a text string in it. </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">10</span> <span style="color: #000066;">-Children</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #009900;">&quot;A Question&quot;</span> <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">42</span> <span style="color: #000066;">-FontWeight</span> Bold <span style="color: #000066;">-Foreground</span> <span style="color: #009900;">&quot;#FF0088&quot;</span> <br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-Inlines</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bold <span style="color: #009900;">&quot;Q. &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Are you starting to dig &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Hyperlink <span style="color: #009900;">&quot;PowerBoots?&quot;</span> <span style="color: #000066;">-NavigateUri</span> http:<span style="color: #66cc66;">//</span>huddledmasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>tag<span style="color: #66cc66;">/</span>powerboots<span style="color: #66cc66;">/</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-On_RequestNavigate</span> <span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">NavigateUri</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">16</span> <span style="color: #000066;">-Inlines</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Span <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-FontWeight</span> Bold <span style="color: #000066;">-Inlines</span> <span style="color: #009900;">&quot;A. &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Leave me alone, I'm hacking here!&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Note: If you want support for the full <a href="http://msdn.microsoft.com/en-us/library/aa970909.aspx">document model</a> (which allows Paragraphs and Lists), you need to use a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentreader.aspx">FlowDocumentReader</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentpageviewer.aspx">FlowDocumentPageViewer</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx">RichTextBox</a>, or a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentscrollviewer.aspx">FlowDocumentScrollViewer</a> ... there&#8217;s lots more information about those <a href="http://msdn.microsoft.com/en-us/library/aa970909.aspx">on msdn</a>.</p>

	<h3>Events</h3>

	<p>If you were paying attention to that previous example, you&#8217;ll notice we just introduced event handling. Event handlers in PowerBoots are specified in much the same way that Properties are.  Their parameter names always start with &#8220;On_&#8221; and they take a script block.  The Hyperlink element in a <span class="caps">WPF</span> window doesn&#8217;t automatically open a browser (because you can use it to change &#8220;pages&#8221; in a <span class="caps">WPF</span> application), so to make simple web links work, you have to handle the &#8220;RequestNavigate&#8221; event as shown above.</p>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots8.png" class="float-right-block" alt="" width="202" height="76" /></p>

	<p>In order to update your user interface when an event triggers, you&#8217;ll need to have a variable that points at the control(s) you want to affect.  You get a <code>$this</code> variable for free which points at the object that caused the event (eg: the Hyperlink in our previous example), but otherwise you need to handle this yourself. You can do that one of two ways: you can set a variable the way you normally would, and then use the variable in the form, or you can specify the variable name using the <code>-OutVariable</code> parameter.  Personally I prefer the latter, as it messes up the flow of code less, but it has the downside that the output variable is always an array, even when there&#8217;s only one item.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:Count <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp;WrapPanel &nbsp;$<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;Push Me&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:Count<span style="color: #66cc66;">++</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$label</span>.<span style="color: #003366;">Content</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;You clicked the button ${global:Count} times!&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$script</span>:label <span style="color: #66cc66;">=</span> Label <span style="color: #009900;">&quot;Nothing pushed so far&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$label</span> <span style="color: #666666; font-style: italic;"># You have to actually write-output the label</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Title</span> <span style="color: #009900;">&quot;Test App&quot;</span> <span style="color: #000066;">-On_Closing</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$global</span>:BootsOutput <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$global</span>:Count; <span style="color: #660033;">rm</span> variable:Count <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>I&#8217;ve made these examples slightly more complicated than they had to be to demonstrate some best practices.  When the Window is closed, the Out-Boots function returns the $BootsOutput variable &#8212; so if you want to <strong>output</strong> something from your gui, you need to set that variable.  You can, of course, access global scope variables using the scope prefix <code>$global:variableName</code>, so you can set many different variables which you read later in your script.  The catch is, sometimes the variables <em>have</em> to be explicitly set to script or even global scope in order to refer to the same variable in all of the event handlers&#8230;</p>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots9.png" class="float-right-block" alt="" width="260" height="84" /></p>

	<p>However, if you want to have this block of code actually <strong>output</strong> something into the pipeline, you&#8217;ll always want to use the <code>$BootsOutput</code> variable.  You can do that directly, the way the example above does, or you can simply use Write-Output! Inside the Out-Boots function, he Write-Output cmdlet just appends to the $BootsOutput variable &#8230; so it works pretty much exactly the way you would expect it to.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;WrapPanel <span style="color: #000066;">-On_Load</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Count</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #333;">&#125;</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;Push Me&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">++</span><span style="color: #660033; font-weight: bold;">$count</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># You have to use array notation ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$block</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Inlines</span>.<span style="color: #660033;">Clear</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$block</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Inlines</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;You clicked the button $count times!&quot;</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #009900;">&quot;Nothing pushed so far&quot;</span> <span style="color: #000066;">-OutVariable</span> script:block <span style="color: #000066;">-VerticalAlignment</span> Center<br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>The first example outputs <strong>just</strong> the count of how many times you clicked.  The second outputs a series of numbers from 1 to however many times you click.  It&#8217;s your choice of how to work with it.</p>

	<h3>We can have fun with colors</h3>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots10.png" class="float-right-block" alt="" width="318" height="286" /></p>

	<p>Boots gives you access to all the capabilities of the Windows Presentation Framework, but in some cases that comes at a cost, because we haven&#8217;t simplified their composability.  So we have RadialGradientBrush and LinearGradientBrush, but you have to specify the GradientStops etc &#8230;</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #000066;">-Background</span> <span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;RadialGradientBrush $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; GradientStop <span style="color: #000066;">-Offset</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">-Color</span> <span style="color: #009900;">&quot;#F00&quot;</span><br />
&nbsp; &nbsp; &nbsp; GradientStop <span style="color: #000066;">-Offset</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Color</span> <span style="color: #009900;">&quot;#F90&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;Label <span style="color: #009900;">&quot;Boots&quot;</span> <span style="color: #000066;">-HorizontalAlignment</span> Center `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-VerticalAlignment</span> Center `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-Foreground</span> White <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">80</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-FontWeight</span> Bold &nbsp;<span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">40</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>We also have</p>

	<h3>So what does an InputBox look like?</h3>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots11.png" class="float-right-block" title="PowerBoots11.png" alt="PowerBoots11.png" width="260" height="86" /></p>

	<p>Well, the simplest possible input box is just a TextBox, with the Width set (if you don&#8217;t set the Width, a TextBox will adjust to fit it&#8217;s contents, which can be really distracting).  All you need to do is this:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;TextBox <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">220</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Title</span> <span style="color: #009900;">&quot;Enter your name&quot;</span> <span style="color: #000066;">-On_Close</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">Text</span> <br />
<span style="color: #333;">&#125;</span> <br />
&nbsp;</div>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots12.png" class="float-right-block" title="PowerBoots12.png" alt="PowerBoots12.png" width="200" height="76" /></p>

	<p>Of course, the problem with that is that it pops up this rather confusing window, when what we really wanted was a prompt, and an &#8220;Ok&#8221; button, and some event handling to make the thing behave the way we expect it to.  So lets try our first complicated form. </p>

	<p>I&#8217;ll warn you ahead of time of one thing I&#8217;m going to do here. I&#8217;m using the &#8220;Border&#8221; element to apply a colored border to the StackPanel (because it doesn&#8217;t have it&#8217;s own border parameters), but then I&#8217;m also using the WindowStyle and AllowsTransparency properties to remove the normal window chrome, creating the bare little popup you see in the screenshot. I handle the mouse down event on the main window to allow the user to drag the window around by clicking anywhere on it (except in the TextBox or on the Button, of course). Now it looks slick, and &#8212;you know&#8212; it works!</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">BootsInput</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Prompt</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Please enter your name:&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Variable</span></span> textBox <span style="color: #000066;">-ErrorAction</span> SilentlyContinue<br />
&nbsp; &nbsp;Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Border <span style="color: #000066;">-BorderThickness</span> <span style="color: #cc66cc;">4</span> <span style="color: #000066;">-BorderBrush</span> <span style="color: #009900;">&quot;#BE8&quot;</span> <span style="color: #000066;">-Background</span> <span style="color: #009900;">&quot;#EFC&quot;</span> <span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">10</span> &nbsp;$<span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label <span style="color: #660033; font-weight: bold;">$Prompt</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TextBox <span style="color: #000066;">-OutVariable</span> global:textbox <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">150</span> <span style="color: #000066;">-On_KeyDown</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Key</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;Return&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$textbox</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Button <span style="color: #009900;">&quot;Ok&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$textbox</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_Load</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$textbox</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Focus</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span> `<br />
&nbsp; &nbsp;<span style="color: #000066;">-WindowStyle</span> None <span style="color: #000066;">-AllowsTransparency</span> <span style="color: #660033; font-weight: bold;">$true</span> `<br />
&nbsp; &nbsp;<span style="color: #000066;">-On_PreviewMouseLeftButtonDown</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Source</span> <span style="color: #000066;">-notmatch</span> <span style="color: #009900;">&quot;.*\.(TextBox|Button)&quot;</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$BootsWindow</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Hopefully you can follow that, although it&#8217;s obviously over the top  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> . We handle the KeyDown event on the TextBox (if the Key is the Return key), and we also handle the click on the Button.  In both cases, we&#8217;ll write out the text that was entered, and use the special $BootsWindow variable to close the window.  We also handle the Load event for the window, to make sure the focus is on the TextBox, so you can just start typing.</p>

	<h3>A final example</h3>

	<p><img src="http://huddledmasses.org/images/PowerBoots/PowerBoots13.png" class="float-right-block" title="PowerBoots13.png" alt="PowerBoots13.png" width="248" height="740" /></p>

	<p>I&#8217;ve got quite a few more examples I want to show off in part two of this tutorial, but to get you thinking about ways to integrate this with your routine tasks, and give you some ideas of what you can do with what you know already, let me give you this example of browsing photos, with a visual indication of how big the image file is. Of course, the point is that you could be visualizing, you know &#8230; anything.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">add-<span style="font-style: normal;">type</span></span> <span style="color: #000066;">-Assembly</span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span> &nbsp;<span style="color: #666666; font-style: italic;"># To get the Double-Click time</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">GraphLabel</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PARAM</span> <span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">String</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Label</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Name&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">String</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Value</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Length&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ScriptBlock</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$DoubleClickAction</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$null</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$max</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$null</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$width</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">200</span>, <br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">double</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$margin</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$DoubleClickTime</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">SystemInformation</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DoubleClickTime</span><span style="color: #333;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;IO&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">PSObject</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$InputObject</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">BEGIN</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$maxx</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$max</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$maxx</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$maxx</span><span style="color: #66cc66;">=</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$InputObject</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #660033; font-weight: bold;">$Value</span> <span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$io</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$InputObject</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## This is the core part of the script ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## For each input, generate a grid panel with a label and a rectangle in the background</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GridPanel <span style="color: #000066;">-tag</span> @<span style="color: #333;">&#123;</span>item<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$io</span>; action<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$DoubleClickAction</span><span style="color: #333;">&#125;</span> <span style="color: #000066;">-width</span> <span style="color: #660033; font-weight: bold;">$Width</span> <span style="color: #000066;">-margin</span> <span style="color: #660033; font-weight: bold;">$margin</span> $<span style="color: #333;">&#40;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Label <span style="color: #660033; font-weight: bold;">$io</span>.<span style="color: #660033; font-weight: bold;">$Label</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rectangle <span style="color: #000066;">-HorizontalAlignment</span> Left <span style="color: #000066;">-Fill</span> <span style="color: #009900;">&quot;#9F00&quot;</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">-Width</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Width</span> <span style="color: #66cc66;">*</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$io</span>.<span style="color: #009900;">&quot;$Value&quot;</span> <span style="color: #66cc66;">/</span> <span style="color: #660033; font-weight: bold;">$maxx</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#41;</span> <span style="color: #000066;">-On_MouseLeftButtonDown</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Tag</span>.<span style="color: #003366;">Action</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #666666; font-style: italic;"># They passed in a doubleclick action, so lets handle it</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$global</span>:ClickTime <span style="color: #000066;">-and</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$ClickTime</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">TotalMilliseconds</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$global</span>:DoubleClickTime<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># We invoke the scriptblock </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># and pass it the original input object </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># and the grid panel object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$This</span>.<span style="color: #003366;">Tag</span>.<span style="color: #003366;">Action</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Tag</span>.<span style="color: #003366;">Item</span> <span style="color: #660033; font-weight: bold;">$this</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$global</span>:ClickTime <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> GraphLabel <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">GraphLabel</span></span><br />
<br />
<span style="color: #666666; font-style: italic;">## Example 1: list of processes with most RAM usage</span><br />
<span style="color: #666666; font-style: italic;">## DoubleClickAction is `kill`</span><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033;">ps</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">sort</span> PM <span style="color: #000066;">-Desc</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-First</span> <span style="color: #cc66cc;">20</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; GraphLabel ProcessName PM <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033;">Kill</span> <span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:panel<span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Children</span>.<span style="color: #003366;">Remove</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp;StackPanel <span style="color: #000066;">-ov</span> global:panel<br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;">## Example 2: list of images, with file size indicated</span><br />
<span style="color: #666666; font-style: italic;">## DoubleClickAction is `open`</span><br />
Boots <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033;">ls</span> ~<span style="color: #66cc66;">/</span>Pictures<span style="color: #66cc66;">/</span> <span style="color: #000066;">-recurse</span> <span style="color: #000066;">-Include</span> <span style="color: #66cc66;">*</span>.<span style="color: #003366;">jpg</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Sort</span> Length <span style="color: #000066;">-Desc</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Max</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$Max</span><span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Length</span><span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; StackPanel <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">200</span> <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">5</span> $<span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Image <span style="color: #000066;">-Source</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">FullName</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GraphLabel Name Length <span style="color: #000066;">-Max</span> <span style="color: #660033; font-weight: bold;">$Max</span> <span style="color: #000066;">-IO</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">FullName</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> WrapPanel <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">800</span><br />
&nbsp;</div>

	<p>Notice that I had to manually handle the concept of a double click, because StackPanels don&#8217;t have a Click or DoubleClick event, just MouseDown and MouseUp.  I could have stuck the stackpanel into something that does, but there&#8217;s really no need.  Also, the <code>[Diagnostics.Process]::Start</code> is the equivalent of typing the name into the run dialog.  I&#8217;m just <em>executing</em> the jpg, which makes it open in the default editor.  It&#8217;s just a sample, after all.</p>

	<h3>End note</h3>

	<p>The current version of Boots does not add threading support, which means that when you run something through Boots, execution of your script stops until the window is closed.  You can get around this somewhat in <a href="http://huddledmasses.org/wpf-from-powershell-updating-windows/">various different ways</a>, but future releases <em>will</em> support running the <span class="caps">WPF</span> window in a separate thread, and even communicating to it &#8230; at the expense of a slightly different syntax. If you need that functionality, feel free to let me know &#8230; I could use some motivation.</p>

	<p>I hope you&#8217;ve enjoyed this tour through PowerBoots, and will be able to start applying it for fun and profit.</p>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powerboots-tutorial-walkthrough/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>PowerBoots: Loading XAML Windows in PowerShell 1.0 or 2.0</title>
		<link>http://joelbennett.net/powerboots-loading-xaml-windows-in-powershell-10-or-20/</link>
		<comments>http://joelbennett.net/powerboots-loading-xaml-windows-in-powershell-10-or-20/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 07:20:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1059</guid>
		<description><![CDATA[Awhile back I wrote a series of posts about WPF From PowerShell From PowerShell&#8221; which were about how you could load XAML in previous PowerShell 2 CTPs to create WPF user interfaces &#8230; a few people have mentioned loading XAML in PowerBoots, and a couple of people have posted other samples showing XAML even since [...]]]></description>
			<content:encoded><![CDATA[	<p>Awhile back I wrote a series of posts about <a href="http://huddledmasses.org/?s=&#34;WPF"><span class="caps">WPF</span> From PowerShell</a> From PowerShell&#8221; which were about how you could load <span class="caps">XAML</span> in previous PowerShell 2 CTPs to create <span class="caps">WPF</span> user interfaces &#8230; a few people have mentioned loading <span class="caps">XAML</span> in <a href="http://boots.CodePlex.com">PowerBoots</a>, and a couple of people have posted other samples showing <span class="caps">XAML</span> even since I published the most recent release, so I figure it&#8217;s time to point out that you really can load that <span class="caps">XAML</span> into Boots, and get all the threading and other support.  </p>

	<p>Just for fun, I&#8217;m going to rehash an earlier post about <a href="http://huddledmasses.org/wpf-from-powershell-updating-windows/">updating windows</a> to show how you can go about this using PowerBoots, and hopefully show that it&#8217;s a little easier (and a lot more async).  Compare and contrast the code in this article with that one, just for fun.</p>

	<h2>This works with any version of PowerShell</h2>

	<p>Unlike the original article, most this code (except where explicitly mentioned) works on either PowerShell v2 with PowerBoots, or PowerShell 1.0 with <strong>PoshWPF</strong>, a snapin that is part of the PowerBoots module but is also released separately&#8230; Also, unlike those previous posts, this does <em>not</em> require you to be running PowerShell.exe with the -<span class="caps">STA</span> switch, since the New-BootsWindow cmdlet takes care of threading for us.</p>

<span id="more-1059"></span>

	<h3>The simple splash screen</h3>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$Splash</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-Async</span> <span style="color: #000066;">-Passthru</span> <span style="color: #000066;">-SourceTemplate</span> @<span style="color: #009900;">&quot;<br />
&lt;window xmlns=&quot;</span>http:<span style="color: #66cc66;">//</span>schemas.<span style="color: #003366;">microsoft</span>.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>winfx<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2006</span><span style="color: #66cc66;">/</span>xaml<span style="color: #66cc66;">/</span>presentation<span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; windowstyle=&quot;</span>None<span style="color: #009900;">&quot; allowstransparency=&quot;</span>True<span style="color: #009900;">&quot; opacity=&quot;</span><span style="color: #cc66cc;">0.8</span><span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; topmost=&quot;</span>True<span style="color: #009900;">&quot; sizetocontent=&quot;</span>WidthAndHeight<span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; windowstartuplocation=&quot;</span>CenterOwner<span style="color: #009900;">&quot; showintaskbar=&quot;</span>False<span style="color: #009900;">&quot;&gt;<br />
&nbsp; &nbsp;&lt;img source=&quot;</span>http:<span style="color: #66cc66;">//</span>dilbert.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>dyn<span style="color: #66cc66;">/</span>str_strip<span style="color: #66cc66;">/</span>000000000<span style="color: #66cc66;">/</span>00000000<span style="color: #66cc66;">/</span>0000000<span style="color: #66cc66;">/</span>000000<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">40000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">41215</span><span style="color: #66cc66;">/</span>41215.<span style="color: #003366;">strip</span>.<span style="color: #003366;">print</span>.<span style="color: #003366;">gif</span><span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; height=&quot;</span><span style="color: #cc66cc;">177</span><span style="color: #009900;">&quot;&gt;<br />
&lt;/window&gt;<br />
&quot;</span>@ <span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Content</span><span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_MouseDown</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;"># Imagine this is your script, working ...</span><br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #cc66cc;">3</span><br />
<span style="color: #666666; font-style: italic;"># And now you're done, and want to close it</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$Splash</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Splash</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span></div>

	<p>This is, of course, not a great example of why you would want to use <span class="caps">XAML</span>, since it reads virtually the same as it would if you wrote it in PowerBoots (if you were on PowerShell v2):</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## This requires PowerBoots which is (as of this writing) is still v2 only...</span><br />
<span style="color: #660033; font-weight: bold;">$window</span> <span style="color: #66cc66;">=</span> Boots <span style="color: #000066;">-Async</span> <span style="color: #000066;">-Passthru</span> &nbsp;<span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;Image <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">177</span> <span style="color: #000066;">-Source</span> http:<span style="color: #66cc66;">//</span>dilbert.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>dyn<span style="color: #66cc66;">/</span>str_strip<span style="color: #66cc66;">/</span>000000000<span style="color: #66cc66;">/</span>00000000<span style="color: #66cc66;">/</span>0000000<span style="color: #66cc66;">/</span>000000<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">40000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">41215</span><span style="color: #66cc66;">/</span>41215.<span style="color: #003366;">strip</span>.<span style="color: #003366;">print</span>.<span style="color: #003366;">gif</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-WindowStyle</span> None <span style="color: #000066;">-AllowsTransparency</span> <span style="color: #660033; font-weight: bold;">$True</span> <span style="color: #000066;">-Opacity</span> <span style="color: #cc66cc;">0.8</span> <span style="color: #000066;">-Topmost</span> <span style="color: #660033; font-weight: bold;">$True</span> <span style="color: #000066;">-WindowStartupLocation</span> CenterOwner <span style="color: #000066;">-ShowInTaskbar</span> <span style="color: #660033; font-weight: bold;">$False</span> <span style="color: #000066;">-On_MouseDown</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Imagine this is your script, working ...</span><br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #cc66cc;">3</span><br />
<span style="color: #666666; font-style: italic;"># And now you're done, and want to close it</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$Splash</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Splash</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span></div>

	<p>The big difference, of course, is that this is fully aync, so unlike the splash screen in the original post, we don&#8217;t have to do any tricks to get it to render, and we are able to add a MouseDown handler so you can drag the splash screen around while you wait for it to go away&#8230;</p>

	<h2>And now, for something completely different</h2>

	<p>Let&#8217;s skip over the very useful information I already wrote about the properties you can set on the &#8220;Window&#8221; object in <span class="caps">WPF</span> (all of which are parameters to New-BootsWindow, by the way) and cut straight to the second example &#8230; I&#8217;ve tweaked an old <span class="caps">WPF</span> sample I had and ported it to PowerShell, <em>and now, adapted it for PoshWpf</em> ... the <a href="http://huddledmasses.org/wpf-from-powershell-updating-windows/clock/" rel="attachment wp-att-528">xaml file is here</a> and I&#8217;ll show you the code just as soon as you take a look at the screenshot (trust me, the code will make more sense if you&#8217;ve seen the output):</p>

	<p><img src="http://huddledmasses.org/images/2008-05-09_0029.png" title="A clock, with working CPU and RAM bars" alt="A clock, with working CPU and RAM bars" width="783" height="312" /></p>

	<p>So basically, the UI is defined in a href=&#8216;http://HuddledMasses.org/wpf-from-powershell-updating-windows/clock/&#8217; rel=&#8216;attachment wp-att-528&#8217;&gt;that xaml file, and all we need to do in the script is update the time, update the <span class="caps">CPU</span> bar, and update the <span class="caps">RAM</span> bar&#8230;</p>

	<h3>Please notice the content block</h3>

	<p>PowerBoots passes the window object itself into the content scriptblock, and the content block <strong>must</strong> output the content you want in the window. Of course, if you&#8217;re loading a fully defined window in <span class="caps">XAML</span>, you don&#8217;t want to change the Window&#8217;s content, but you have to return something, so you should just return &#8230; the content that&#8217;s already in the window. Ie: <code>$Args[0].Content</code></p>

	<p>One other point, for those of you who are going to become <span class="caps">XAML</span> masters. In PowerBoots you can define just the styles and templates as resources in a separate <span class="caps">XAML</span> file which you can load with the <code>Add-BootsTemplate</code>, and then you can still create the UI in <span class="caps">XAML</span> or useing PowerBoots syntax.  In my case, this <span class="caps">XAML</span> was edited  using <a href="http://www.kaxaml.com/">kaxaml</a> (it could have been Expression Blend, or Visual Studio), and kaxaml doesn&#8217;t make it easy to separate out resource files, so I just left it all in one file:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Initializing Performance Counters, please have patience&quot;</span> <span style="color: #000066;">-fore</span> Cyan<br />
<span style="color: #666666; font-style: italic;">### Import PoshWpf module</span><br />
<span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> PowerBoots<br />
<span style="color: #666666; font-style: italic;">### Or, on v1:</span><br />
<span style="color: #666666; font-style: italic;"># Add-PSSnapin PoshWpf</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$script</span>:cpu <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Diagnostics</span>.<span style="color: #003366;">PerformanceCounter</span> <span style="color: #009900;">&quot;Processor&quot;</span>, <span style="color: #009900;">&quot;% Processor Time&quot;</span>, <span style="color: #009900;">&quot;_Total&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$script</span>:ram <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Diagnostics</span>.<span style="color: #003366;">PerformanceCounter</span> <span style="color: #009900;">&quot;Memory&quot;</span>, <span style="color: #009900;">&quot;Available KBytes&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## get initial values, because the counters don't work until the second call</span><br />
<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$script</span>:cpu.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$script</span>:ram.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$script</span>:maxram <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033;">gwmi</span> Win32_OperatingSystem<span style="color: #333;">&#41;</span>.<span style="color: #003366;">TotalVisibleMemorySize</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Loading XAML window...&quot;</span> <span style="color: #000066;">-fore</span> Cyan<br />
<span style="color: #666666; font-style: italic;">## Load the XAML and show the window. It won't be updating itself yet...</span><br />
<span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-Async</span> <span style="color: #000066;">-Passthru</span> <span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Content</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-FileTemplate</span> <span style="color: #009900;">&quot;C:\Users\Joel\Documents\WindowsPowerShell\Scripts\Demo\clock.xaml&quot;</span> <br />
<br />
<span style="color: #666666; font-style: italic;">## Create a script block which will update the UI by changing the Resources!</span><br />
<span style="color: #660033; font-weight: bold;">$counter</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #660033; font-weight: bold;">$updateBlock</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Update the clock</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;Time&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;hh:MM.ss&quot;</span><span style="color: #333;">&#41;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># We only want to update the counters at most once a second</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Otherwise their values are invalid and ...</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># The CPU counter fluctuates from 0 to the real number</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$counter</span><span style="color: #66cc66;">++</span> <span style="color: #000066;">-eq</span> <span style="color: #cc66cc;">4</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$counter</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the CPU counter with the absolute value and the percentage</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$cu</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$cpu</span>.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">CpuP</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$cu</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">100</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Cpu</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;{0:0.0}%&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$cu</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the RAM counter with the absolute value and the percentage</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$rm</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$ram</span>.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">RamP</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$rm</span> <span style="color: #66cc66;">/</span> <span style="color: #660033; font-weight: bold;">$maxram</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Ram</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;{0:0.00}Mb&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$rm</span><span style="color: #66cc66;">/</span>1MB<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Now we need to call that scriptblock on a timer. That's easy, but it</span><br />
<span style="color: #666666; font-style: italic;">## must be done on the window's thread, so we use Invoke-BootsWindow.</span><br />
<span style="color: #666666; font-style: italic;">## Notice the first argument is the window we want to run the script in</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## We'll create a timer</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:timer <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Threading</span>.<span style="color: #003366;">DispatcherTimer</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Which will fire 4 times every second</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Interval</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>TimeSpan<span style="color: #333;">&#93;</span></span><span style="color: #009900;">&quot;0:0:0.25&quot;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## And will invoke the $updateBlock</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Add_Tick</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$updateBlock</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Now start the timer running</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## And just like that, the $UpdateBlock is running 4x a second</span><br />
<span style="color: #666666; font-style: italic;">## and the clock is working. &nbsp;Pretty cool, right?</span><br />
<span style="color: #666666; font-style: italic;">## what's really cool is ... we still have full control in the console</span><br />
<span style="color: #666666; font-style: italic;">## so we can add these events afterward:</span><br />
<br />
<span style="color: #666666; font-style: italic;">## If we wanted to, say, handle mouse events to let you drag the window or close it ...</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Add_MouseLeftButtonDown</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># WPF Magic!</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Add_MouseRightButtonDown</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Stop</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp;<span style="color: #666666; font-style: italic;"># we'd like to stop that timer now, thanks.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># and close the windows</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<h2>So, meet Register-BootsEvent</h2>

	<p>If you&#8217;ve read the previous PowerBoots articles, you know that you can specify events as parameters to the PowerBoots functions, like <code>-On_Click</code> or <code>-On_MouseLeftButtonDown</code>, but in the case where you&#8217;re loading <span class="caps">XAML</span>, you can&#8217;t specify events in the <span class="caps">XAML</span> (WPF&#8217;s XamlReader won&#8217;t read <span class="caps">XAML</span> with events on it, or even with the <code>Class</code> specified, because you can&#8217;t use the associated code file anyway).  </p>

	<p>In any case, what I&#8217;m adding to the next version of PowerBoots/PoshWpf are two functions: </p>

	<ol>
		<li>Find-BootsControl takes a boots window and the <strong>Name</strong> of a control, and will (try to) find the named control. Of course, for that to work, you need to know the name of the control (your best bet is to name it yourself, but in the next version I may also issue names for elements as they are created if you don&#8217;t specify the name).</li>
	</ol>
	<ol>
		<li>Register-BootsEvent takes either a control, OR a boots window and control name, plus an event name and a ScriptBlock.  If you pass the window and name, it calls Find-BootsControl to find the control; once it has a control, it registers the ScriptBlock as a handler for the specified event.</li>
	</ol>

	<p>For now, both of these require that you&#8217;ve assigned a name to the control, but I&#8217;m working on setting things up so controls would be automatically named (based on their type, as with the old WinForms designer: like Button1, Button2, etc.), or allowing an XPath-like selection without named controls. In any case, this will work nicely for generating event handlers for <span class="caps">WPF</span> imported from <span class="caps">XAML</span>, and you can even specify the element directly (ie: if you don&#8217;t specify the -Name parameter, the event will be hooked on whatever element you pass in) so that last block of code can be written like this in the next release:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## If we wanted to, say, handle mouse events to let you drag the window or close it ...</span><br />
<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">BootsEvent</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #000066;">-Event</span> MouseLeftButtonDown <span style="color: #000066;">-Action</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># WPF Magic!</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">BootsEvent</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #000066;">-Event</span> MouseRightButtonDown <span style="color: #000066;">-Action</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Stop</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp;<span style="color: #666666; font-style: italic;"># we'd like to stop that timer now, thanks.</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># and close the windows</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/powerboots-loading-xaml-windows-in-powershell-10-or-20/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

