PowerShell – Basic Form

email me

Snapshot

The form…with dropdown selection, buttons, a label, an icon, and transparency.

 
Code

Clear-Host

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

# Create Form
$Form = New-Object system.Windows.Forms.Form 
$Form.Size = New-Object System.Drawing.Size(400,300) 
#$Form.Width = 400 
#$Form.Height = 300 
$Form.MaximizeBox = $false 
$Form.StartPosition = "CenterScreen" 
$Form.FormBorderStyle = 'Fixed3D' 
$Form.Text = "My Application" 

$Form.AutoSizeMode = "GrowAndShrink"    # or GrowOnly
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"            # Maximized, Minimized, Normal
$Form.SizeGripStyle = "Hide"            # Auto, Hide, Show
$Form.ShowInTaskbar = $True
$Form.Opacity = 0.7                     # 1.0 is fully opaque; 0.0 is invisible
$Form.StartPosition = "CenterScreen"    # CenterScreen, Manual, WindowsDefaultLocation, WindowsDefaultBounds, CenterParent


# Icon
$Icon = New-Object system.drawing.icon ("C:\Program Files (x86)\Microsoft Office\Office15\Groove\ToolIcons\COMPUTER.ICO")
$Form.Icon = $Icon

# Label
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "This form is very simple."
$Label.AutoSize = $True
$Label.Location = New-Object System.Drawing.Size(120,50) 
$Font = New-Object System.Drawing.Font("Calibri",10,[System.Drawing.FontStyle]::Regular) 
$Form.Font = $Font
$Form.Controls.Add($Label)


# Drop Down Selection

# Create datatable to bind a combobox
$datatable = New-Object system.Data.DataTable

# Define Columns
$col1 = New-Object system.Data.DataColumn "Value",([string])
$col2 = New-Object system.Data.DataColumn "Text",([string])


# Add columns to Datatable
$datatable.columns.add($col1)
$datatable.columns.add($col2)
		
# 1 Create Row
$datarow1 = $datatable.NewRow()

# Enter data in row
$datarow1.Value = "Value 1"
$datarow1.Text = "Text 1"

# Add row to datatable
$datatable.Rows.Add($datarow1)


# 2 Create Row
$datarow2 = $datatable.NewRow()

#Enter data in the row
$datarow2.Value = "Value 2"
$datarow2.Text = "Text 2"

# Add the row to the datatable
$datatable.Rows.Add($datarow2)


# 3 Create Row
$datarow3 = $datatable.NewRow()

# Enter Data in row
$datarow3.Value = "Value 3"
$datarow3.Text = "Text 3"

# Add Row to datatable
$datatable.Rows.Add($datarow3)

# Create combobox
$combobox = New-Object System.Windows.Forms.ComboBox		
$combobox.Add_SelectedIndexChanged({
		#output the selected value and text
		write-host $combobox.SelectedItem["Value"] $combobox.SelectedItem["Text"]
})

# Clear Combo before bind
$combobox.Items.Clear()

# Bind Combobox to datatable
$combobox.ValueMember = "Value"
$combobox.DisplayMember = "Text"
$combobox.Datasource = $datatable

# Add Combobox to form
$form.Controls.Add($combobox)	


# Button 1
$Okbutton1 = New-Object System.Windows.Forms.Button 
$Okbutton1.Location = New-Object System.Drawing.Size(80,80)
$Okbutton1.Size = New-Object System.Drawing.Size(100,30)
$Okbutton1.Text = "OK1"
$Okbutton1.Add_Click({$Form.Close()
# do this with button
Write-Host "OK Button 1"}) 
$Form.Controls.Add($Okbutton1)

# Button 2
$Okbutton2 = New-Object System.Windows.Forms.Button 
$Okbutton2.Location = New-Object System.Drawing.Size(200,80)
$Okbutton2.Size = New-Object System.Drawing.Size(100,30)
$Okbutton2.Text = "OK2"
$Okbutton2.Add_Click({$Form.Close() 
# do this with button
Write-Host "OK Button 2"}) 
$Form.Controls.Add($Okbutton2)

# Show form
[void]$form.showdialog()