Wednesday, December 06, 2006

Quick'n'dirty user interface

The example below shows a quick way to use a standard windows messagebox as a user interface, through Windows Forms.

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

$myTitle = "Important Question"
$myText1 = "Is PowerShell useful to you ?"

$a = [Windows.Forms.MessageBox]::Show($myText1, $myTitle,`
    [Windows.Forms.MessageBoxButtons]::YesNoCancel, `
    [Windows.Forms.MessageBoxIcon]::Question)
$myText2 = "You answered: $a"
$b = [Windows.Forms.MessageBox]::Show($myText2, $myTitle, `
    [Windows.Forms.MessageBoxButtons]::Ok, `
    [Windows.Forms.MessageBoxIcon]::Information)

2 comments:

Jeffrey Snover said...

"Use the Type Coercion Luke"
Instead of:
$a = [Windows.Forms.MessageBox]::Show($myText1, $myTitle,`
[Windows.Forms.MessageBoxButtons]::YesNoCancel, `
[Windows.Forms.MessageBoxIcon]::Question)

Try:
$a = [Windows.Forms.MessageBox]::Show($myText1, $myTitle,`
"YesNoCancel", `
"Question")

Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Jeffrey Snover said...

While we are at it, did you realize that you could do this:

$mb=[Windows.Forms.MessageBox]
$a = $mb::Show($myText1, $myTitle,`
"YesNoCancel", `
"Question")


This should save you some typing.

Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx