Powershell – GUI – Playing with Windows Forms

If you are wanting to provide your Powershell scripts in GUI form so that other people can use them quickly and easily, then this post contains some of the basic elements required to put together a functional GUI.

Code description

  1. Sets the configuration for the form window
  2. Adds the elements to the form (Dropdown1 , InputBox1 , OutputBox1, CheckBox1 , Label1 and Button1)
  3. Adds a function for what to do once the Button is clicked
    1. Content of Dropdown1 will be appended to OutputBox1
    2. Content of InputBox1 will be appended to OutputBox1
    3. Status of CheckBox1 will be appended to OutputBox1
  4. Creates the form

Code

function makeForm {

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

#Form Settings (Size, Background Image, Font, Window title)
$Form = New-Object System.Windows.Forms.Form 
$Form.Size = New-Object System.Drawing.Size(600 ,400) 
$Form.Font = New-Object System.Drawing.Font("Arial",9,[System.Drawing.FontStyle]::Regular)
$Form.BackColor = "#B2B3BF"
$Form.text = "Test Form"

#Adding close by hitting Escape Key
$Form.KeyPreview = $True
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
{$Form.Close()}})

#Elements
#Dropdown
$Dropdown1 = New-Object System.Windows.Forms.ComboBox
$Dropdown1.Location = New-Object System.Drawing.Size(10,10) 
$Dropdown1.Size = New-Object System.Drawing.Size(500,20) 
$Dropdown1.Visible = $True
$Dropdown1.DropDownHeight = 200 
$Form.Controls.Add($Dropdown1)

$Dropdown1.Items.Add("Dropdown")
$Dropdown1.Items.Add("Test1")
$Dropdown1.Items.Add("Test2")

$Dropdown1.SelectedItem = $Dropdown1.Items[0]

#Textbox
$InputBox1 = New-Object System.Windows.Forms.TextBox
$InputBox1.Location = New-Object System.Drawing.Size(10,40) 
$InputBox1.Size = New-Object System.Drawing.Size(500,20) 
$InputBox1.Visible = $True
$InputBox1.MultiLine = $False 
$InputBox1.Text = "Text Box"
$Form.Controls.Add($InputBox1)

$OutputBox1 = New-Object System.Windows.Forms.TextBox
$OutputBox1.Location = New-Object System.Drawing.Size(10,200) 
$OutputBox1.Size = New-Object System.Drawing.Size(500,100) 
$OutputBox1.Visible = $True
$OutputBox1.MultiLine = $True 
$Form.Controls.Add($OutputBox1)

#Checkbox
$Checkbox1 = New-Object System.Windows.Forms.Checkbox 
$Checkbox1.Location = New-Object System.Drawing.Size(10,70)
$Checkbox1.Size = New-Object System.Drawing.Size(20,20) 
$Checkbox1.Enabled = $True
$Checkbox1.Visible = $True
$Checkbox1.Checked = $True
$Form.Controls.Add($Checkbox1) 

#TextLabel
$Label1 = New-Object System.Windows.Forms.Label 
$Label1.Location = New-Object System.Drawing.Size(10,100) 
$Label1.Size = New-Object System.Drawing.Size(100,20) 
$Label1.Text = "Label"
$Label1.TextAlign = "MiddleLeft"
$Label1.Visible = $True
$Label1.ForeColor = "#007F7A"
$Label1.BackColor = "#FFFFFF"
$Form.Controls.Add($Label1) 

#Button
$Button1 = New-Object System.Windows.Forms.Button 
$Button1.Location = New-Object System.Drawing.Size(10,130)
$Button1.Size = New-Object System.Drawing.Size(100,20) 
$Button1.Text = "Submit" 
$Button1.BackColor = "#FFFFFF"
$Button1.Visible = $True
$Button1.Add_Click({Button}) 
$Form.Controls.Add($Button1) 

$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

}

Function Button {

$OutputBox1.Text += $Dropdown1.Text | Out-String
$OutputBox1.Text += $InputBox1.Text | Out-String
If ($Checkbox1.Checked -eq $True) {$OutputBox1.Text += "Checkbox checked" | Out-String} Else {$OutputBox1.Text += "Checkbox not checked" | Out-String}

}

makeForm

GUI.png

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s