Monday, 27 October 2014

How to build a form in PowerShell ?

Windows Forms (Win Forms)

https://gallery.technet.microsoft.com/How-to-build-a-form-in-7e343ba3

Windows Forms is a set of managed libraries in .NET Framework. Win-form is designed to develop client applications and it provides different kind of controls, such as text-boxes, buttons, and web pages along with options to create custom controls.
Here I am showing how to create a GUI using win-from... This is an older way of form creation, but the base of all GUI applications.

First Steps to create a form

Loading Assemblies : In some cases, the required class for form may not be loaded into powershell by default. So we have to load it before the creation of form. There are two main classes which may required in our form creation (It may work without loading assemblies in Powershell hosts, but it won't work if you are converting PS Scripts to .Exe and running). Best practice is to load those class in the initial stage to avoid errors
       
001
002
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing


Second Steps to create a form

Object Creation : We have to create a new object. This object type may differ based on our requirement.Once you create a from you have to call the from.Below is some basic examples to create a form

PART -1  Basics of form creation :


001
$Form = New-Object system.Windows.Forms.Form

Do a get-member ( $Form | gm ) to view all the property and method of this object and call the form to view the GUI of form using the below command


001
$Form.ShowDialog()
      
Now Combine all together and run from PowerShel ISE or any other host and see the out put.


001
002
003
004
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Form = New-Object system.Windows.Forms.Form
$Form.ShowDialog()
















The above form is entirely blank as we have't added extra object into our form. So now we are going to add  data and doing some customization in form ($form). Follow the below steps to do that.

Selecting fOrm size is very important. We should decide a comfortable form size for our application. Either you can use the auto-size or custom size as per your requirement


001
002
003
004
005
006
007
$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(400,200)
#You can use the below method as well
#$Form.Width = 400#$Form.Height = 200#
$Form.AutoSize = $True
$Form.AutoSizeMode = "GrowAndShrink"
$Form.Text = "My Application"

# If you are using auto size with auto-size mode growandshrink, form size will fluctuate as per the data that you are adding to form..

Label Creation and customization ::


001
002
003
004
005
$Label = New-Object System.Windows.Forms.Label
$Label.AutoSize = $true
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
$Label.Text = "This is My Test App"

###  Method of Adding $Label object into $From object


001
002
$Form.Controls.Add($Label)
$Form.ShowDialog()













What we learned in 1st Part ???

Loading assembly
Creating different object for forms
Customizing form ( By giving size and text )
Creating and customizing label And finally calling the form
________________________________________________

PART - 2  Form Customization: 

Now we are going to create a form which includes >>>
Creation of form closing actions
Form button creation
Button creation
# Base form creation


001
002
003
004
$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(400,200)
$Form.Text = "My Application"

# 1 - Creation of form's closing actions

001
002
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$Form.Close()}})
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$Form.Close()}})

#As per the above example, form will be closed if you press Enter or Escape button. You can include letters as well to close the form (We should give only capital letters). As per the below example, form will be closed if you press V letter on the keyboard.

001
$Form.Add_KeyDown({if ($_.KeyCode -eq "V") {$Form.Close()}})

# 2 - Form Button Creation
# We have to create a .ICO image for our form button. Either you can generate from Photoshop or you can create from online icon creation website.

001
002
$formIcon = New-Object system.drawing.icon ("C:\vivek\red.ico")
$form.Icon = $formicon

# 3 - Button Creation

001
002
003
004
005
006
007
008
$Okbutton = New-Object System.Windows.Forms.Button
$Okbutton.Location = New-Object System.Drawing.Size(125,50)
$Okbutton.Size = New-Object System.Drawing.Size(100,50)
$Okbutton.Text = "OK"
$Okbutton.Add_Click({$Form.Close()})
$Form.Controls.Add($Okbutton)
$Form.ShowDialog()

Now our form is ready with Form icon and Button














Putting all the command together: PART - 1 And PART - 2

I have comment out the Form ICON lines... You can enable if you have a downloaded ICON pic.


001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(400,200)
$Form.AutoSizeMode = "GrowAndShrink"
$Form.Text = "My Application"


$Label = New-Object System.Windows.Forms.Label
$Label.AutoSize = $true
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font
$Label.Text = "This is My Test App"
$Form.Controls.Add($Label)


$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$Form.Close()}})
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$Form.Close()}})


#$formIcon = New-Object system.drawing.icon ("C:\vivek\red.ico")
#$form.Icon = $formicon


$Okbutton = New-Object System.Windows.Forms.Button
$Okbutton.Location = New-Object System.Drawing.Size(125,50)
$Okbutton.Size = New-Object System.Drawing.Size(100,50)
$Okbutton.Text = "OK"

$Okbutton.Add_Click({$Form.Close()})
$Form.Controls.Add($Okbutton)


$Form.ShowDialog()

PART - 3  Creation of Button's Click Action: 

We have different option to connect button to our script. In previous part, I have used close() command in button click action to close the form. Now I am going to change the click action...


001
$Okbutton = New-Object System.Windows.Forms.Button

Method - 1
Directly using CMDLET

001
002
003
004
005
006
$Okbutton = New-Object System.Windows.Forms.Button
$Okbutton.Location = New-Object System.Drawing.Size(125,50)
$Okbutton.Size = New-Object System.Drawing.Size(200,50)
$Okbutton.Text = "Get-Service"
$Okbutton.Add_Click({Get-Service | Out-GridView})
$Form.Controls.Add($Okbutton)

Method - 2
Using function name by converting script to function.... Function should be on top of script

001
002
003
004
005
006
007
008
009
010
function Service
{
Get-Service | Out-GridView
}
$Okbutton = New-Object System.Windows.Forms.Button
$Okbutton.Location = New-Object System.Drawing.Size(125,50)
$Okbutton.Size = New-Object System.Drawing.Size(200,50)
$Okbutton.Text = "Get-Service"
$Okbutton.Add_Click({Service})
$Form.Controls.Add($Okbutton)

Method - 3
Calling script file (PS1)

001
002
003
004
005
006
$Okbutton.Location = New-Object System.Drawing.Size(125,50)
$Okbutton.Size = New-Object System.Drawing.Size(200,50)
$Okbutton.Text = "Get-Service"
$Okbutton.Add_Click({
Invoke-Expression -Command "C:\Vivek\service.ps1"
})

PART - 4  Creation and customization of Input and Output Box:





2 comments:

  1. Nice article vivek--
    Bob-

    ReplyDelete
  2. Nice Blog..
    thanks for posting.

    I am also started building powershell GUI scripts and this blog will help me to create and understand more about windows form.

    ReplyDelete