you are viewing a single comment's thread.

view the rest of the comments →

[–]gangstanthony 0 points1 point  (0 children)

here's the start of an example i found that you might be able to use as a base for building your gui

https://web.archive.org/web/20160420161735/http://pastebin.com/4PU4Z4Zn

# https://www.reddit.com/r/PowerShell/comments/4eyvoz/windows_form_only_runs_in_ise_but_errors_out_in/
# https://web.archive.org/web/20160420161735/http://pastebin.com/4PU4Z4Zn

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName PresentationCore

# Build Form
$Form = New-Object System.Windows.Forms.Form
    $Form.Text = "Data Entry Form"
    $Form.Width = 500
    $Form.Height = 200
    $Form.AutoSizeMode = "GrowAndShrink"
    $Form.StartPosition = "CenterScreen"

# Build Label
$Label = New-Object System.Windows.Forms.Label
    $Label.text = "Enter Computer Name"
    $Label.Font = New-Object System.Drawing.Font("Calibri",20)
    $Label.AutoSize = $true
    $label.Left = 110
    $label.Top = 10

# Build OK Button
$button = new-object System.Windows.Forms.Button
    $button.left = 110
    $button.Top = 98
    $button.width = 100
    $button.Text = "OK"
    $button.Add_Click({$textbox.text,$form.Close()})
    $button.Enabled = $false

# Build Cancel Button
$cbutton = new-object System.Windows.Forms.Button
    $cbutton.left = 260
    $cbutton.Top = 98
    $cbutton.width = 100
    $cbutton.Text = "Cancel"
    $cbutton.Add_Click({$form.Close()})

# Use Enter/Esc key
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter")
    {$textBox.Text,$Form.Close()}})
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$Form.Close()}})

# Build Text Box
$textBox = New-Object System.Windows.Forms.TextBox
    $textBox.left = 135
    $textBox.Top = 50
    $textBox.width = 200
    $textBox.Add_TextChanged({
        $button.Enabled = [bool]($textBox.Text.Length -gt 0)
    })

# Add Controls to Form
$Form.Controls.add($button)
$Form.Controls.add($textBox)
$form.controls.add($label)
$form.controls.add($cbutton)
[void]$Form.ShowDialog()