<?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>PowerShell.nu &#187; Windows.Forms</title>
	<atom:link href="http://www.powershell.nu/tag/windowsforms/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.powershell.nu</link>
	<description></description>
	<lastBuildDate>Wed, 14 Jul 2010 22:17:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Drop Down menu using Windows.Forms</title>
		<link>http://www.powershell.nu/2009/01/21/dropdown-menu-using-windowsforms/</link>
		<comments>http://www.powershell.nu/2009/01/21/dropdown-menu-using-windowsforms/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 18:02:38 +0000</pubDate>
		<dc:creator>Niklas Goude</dc:creator>
				<category><![CDATA[Windows.Forms]]></category>
		<category><![CDATA[GUI]]></category>

		<guid isPermaLink="false">http://www.powershell.nu/?p=370</guid>
		<description><![CDATA[When creating forms and GUI through PowerShell you can use the .NET Class System.Windows.Forms. This example will describe how to make a basic dopdown form and return the selected value to PowerShell. First we create an Array holding the DopDown values. PS > [array]$DropDownArray = "First Choice", "Second Choice", "Third Choice" The Array contains 3 [...]]]></description>
			<content:encoded><![CDATA[<p>When creating forms and GUI through PowerShell you can use the .NET Class System.Windows.Forms. This example will describe how to make a basic dopdown form and return the selected value to PowerShell. </p>
<p />
First we create an Array holding the DopDown values.</p>
<p />
<pre>
<strong>
PS > [array]$DropDownArray = "First Choice",
"Second Choice",
"Third Choice"
</strong>
</pre>
<p />
The Array contains 3 choices. You can modify this to your own specifications or you can retrieve a list from an Internet Page, Active-Directory or whatever you want to display. Just make sure that its presented in an Array ( put [array] in front of the variable holding the Array to assure that the object will be an Array).</p>
<p />
Next we&#8217;ll create a function that defines what will happen after the Form Choice has been made.</p>
<p />
<pre>
<strong>
PS > function Return-DropDown {

	$Choice = $DropDown.SelectedItem.ToString()
	$Form.Close()
	Write-Host $Choice

}
</strong>
</pre>
<p />
The function Stores the Choice in a variable and converts it To a String Value, then it closes the form and finally returns the Choice made. Now all we need to do is create a Form.</p>
<p />
Step one in creating a form is making PowerShell aware of the Windows.Forms .NET class. We do this by loading the Class into the Global Assembly Cache. We also load the System.Drawing Class since it&#8217;s useful when working with Forms.</p>
<p />
<pre>
<strong>
PS > [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

PS > [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
</strong>
</pre>
<p />
You can pipe it to Out-Null if you don&#8217;t want the returnvalue displayed.</p>
<p />
Now we can create our form! First we create the body of the Form. There are alot of properties that are settable but we&#8217;ll keep it simple in this example.</p>
<p />
<pre>
<strong>
PS > $Form = New-Object System.Windows.Forms.Form

PS > $Form.width = 300
PS > $Form.height = 150
PS > $Form.Text = ”DropDown”
</strong>
</pre>
<p />
Now that we have a body for our form we can create a Dropdown list. Here we will define Size and Location through System.Drawing. It lets us set the location based on X and Y coordinates where 0,0 is top left corner. The DropDown list is created through Windows.Forms.ComboBox.</p>
<p />
<pre>
<strong>
PS > $DropDown = new-object System.Windows.Forms.ComboBox
PS > $DropDown.Location = new-object System.Drawing.Size(100,10)
PS > $DropDown.Size = new-object System.Drawing.Size(130,30)
</strong>
</pre>
<p />
To add our Array to the DropDown List we have to loop through it and use the Add() method on each value in the Array.</p>
<p />
<pre>
<strong>
PS > ForEach ($Item in $DropDownArray) {
	$DropDown.Items.Add($Item)
}
</strong>
</pre>
<p />
Now we can add the DropDown List to our Form. This is done through the Controls.Add() method on the $Form object.</p>
<p />
<pre>
<strong>
PS > $Form.Controls.Add($DropDown)
</strong>
</pre>
<p />
Adding a Label to the DropDown menu is done through Windows.Forms.Label. By changing the Location property you can move the label around. The Text property sets the Text shown in the Label.</p>
<p />
<pre>
<strong>
PS > $DropDownLabel = new-object System.Windows.Forms.Label
PS > $DropDownLabel.Location = new-object System.Drawing.Size(10,10)
PS > $DropDownLabel.size = new-object System.Drawing.Size(100,20)
PS > $DropDownLabel.Text = "Items"
PS > $Form.Controls.Add($DropDownLabel)
</strong>
</pre>
<p />
Almost done, now all we need is a confirmation button that executes the function we made earlier. The button is made through Windows.Forms.Button and the the Add_Click() method is used to tell the button what to do when you click on it.</p>
<p />
<pre>
<strong>
PS > $Button = new-object System.Windows.Forms.Button
PS > $Button.Location = new-object System.Drawing.Size(100,50)
PS > $Button.Size = new-object System.Drawing.Size(100,20)
PS > $Button.Text = "Select an Item"
PS > $Button.Add_Click({Return-DropDown})
PS > $form.Controls.Add($Button)
</strong>
</pre>
<p />
Now all we have to do is start the Form. First we activate it and then use the ShowDialog() method to display it on screen.</p>
<p />
<pre>
<strong>
PS > $Form.Add_Shown({$Form.Activate()})
PS > $Form.ShowDialog()
</strong>
</pre>
<p />
And here is what the form looks like:</p>
<p />
<img src="http://www.powershell.nu/wp-content/uploads/2009/01/windowsforms-01.jpg" alt="windowsforms-01" title="windowsforms-01" width="372" height="210" class="alignnone size-full wp-image-371" /></p>
<p />
Below is the code used in this example</p>
<p />
<pre>
<strong>
# Edit This item to change the DropDown Values

[array]$DropDownArray = "First Choice", "Second Choice", "Third Choice"

# This Function Returns the Selected Value and Closes the Form

function Return-DropDown {

	$Choice = $DropDown.SelectedItem.ToString()
	$Form.Close()
	Write-Host $Choice

}

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$Form = New-Object System.Windows.Forms.Form

$Form.width = 300
$Form.height = 150
$Form.Text = ”DropDown”

$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)

ForEach ($Item in $DropDownArray) {
	$DropDown.Items.Add($Item)
}

$Form.Controls.Add($DropDown)

$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,20)
$DropDownLabel.Text = "Items"
$Form.Controls.Add($DropDownLabel)

$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Select an Item"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)

$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
</strong>
</pre>
<p />
]]></content:encoded>
			<wfw:commentRss>http://www.powershell.nu/2009/01/21/dropdown-menu-using-windowsforms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
