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.

Loading Assemblies is the first step in creating a windows form. There are two main assemblies that are required in our form creation (It may work without loading assemblies in Powershell hosts like ISE, but it won't work if you are converting PS Scripts to .Exe and running desperately). The 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

Object Creation: We have to create a new object. This object type may differ based on our requirements. Once you create a form you have to call the form. Below is the basic example of a new object creation for a form

PART -1  Basics of form creation


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

Get member cmdlet ( $Form | gm ) will provide the details of property and method of this object and  we can call the form to view the GUI using the below cmdlet


001
$Form.ShowDialog()

Now Combine all together and run it from PowerShell ISE or any other host and see the output.


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 a blank one as we have not added any items on it, now we are going to add some controls on the form. Follow the below steps.

I am defining the size of the form using the below cmdlet, you can see I have used multiple ways for defining the form size, also there is an option to set the auto-size option.


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 automatically adjust as per the data\controls 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 call the form for GUI
________________________________________________

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, the 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, the 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 websites.

Please note the below cmdlet which is pointing to a path of the icon file.

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 commented out the Form ICON lines... You can enable if you have a downloaded ICON pic by providing a proper name and icon's path


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 options to connect the button to our script. In the 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

Use the cmdlet directly inside the script block

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 - We can define our script under a function and then we can call the function inside the click action. once you click on the button the function will be called and execute.

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) - Using this method we can call a PS1 file that is saved on your machine when you click on any controls (Buttons), in the back end the script you defined will be called and execute.

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"
})


Thanks for reading, I will be posting the Form creation using XAML and WPF soon.