PowerShell: Building a Form

(My Article for Weekly PowerShell Column in office e-mag)

Today we learn about a surprising feature of PowerShell: forms. Many people think that PS is just another bro of cmd.exe and can’t do much fun with GUI. Hmmm…they are simply wrong. Since PS is based on .NET platform, it can access any .NET base class easily and Forms are no exception. So let’s try something into it.

Before calling any .NET namespace, we need to load that DLL into PS session. And this can be done with following commands:

[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null

[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

For creating a form object, try this:

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

You can set various properties of form object directly. E.g. Name, text:

$form1.Text = "Ankit Form"

$form1.Name = "form1"

For a button object, try it:

$button1 = New-Object System.Windows.Forms.Button

$button1.Name = "button1"

 

clip_image002

 

We will discuss various aspects of forms in coming posts. For now, if you wanna play with some form code, try this PS Code:

 

function GenerateForm {

[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

$form1 = New-Object System.Windows.Forms.Form
$button1 = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

$button1_OnClick=
{
[System.Windows.Forms.MessageBox]::Show("Now call Police!")

}

$OnLoadForm_StateCorrection=
{
    $form1.WindowState = $InitialFormWindowState
}

$form1.Text = "Ankit Form"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 284
$System_Drawing_Size.Height = 262
$form1.ClientSize = $System_Drawing_Size

$button1.TabIndex = 0
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 104
$System_Drawing_Size.Height = 35
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True

$button1.Text = "Kill me!"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 89
$System_Drawing_Point.Y = 102
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($button1_OnClick)

$form1.Controls.Add($button1)

$InitialFormWindowState = $form1.WindowState

$form1.add_Load($OnLoadForm_StateCorrection)

$form1.ShowDialog()| Out-Null

}

#Call the Function
GenerateForm

  • Share/Bookmark
2 Comments Post a Comment
  1. Jeffrey Snover says:

    If you like THAT, you should check THIS out:
    http://blog.sapien.com/index.php/2008/11/03/free-primalforms-tool-for-powershell-released/

    It should knock your socks off!
    :-)
    Experiment! Enjoy! Engage!

    Jeffrey Snover [MSFT]
    Distinguished Engineer
    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

  2. Ankit says:

    Thanks Jeffrey.. I installed primalforms and now playing with it :)

Leave a Reply




Categories

Oldies

My Tweets

Follow @ankitwww (88 followers)

My Visitors