Power Shell UI Forms

 # Load the required assembly

Add-Type -AssemblyName System.Windows.Forms


# Create the main form

$form = New-Object System.Windows.Forms.Form

$form.Text = "Sample PowerShell GUI"

$form.Size = New-Object System.Drawing.Size(300, 200)

$form.StartPosition = "CenterScreen"


# Create a label

$label = New-Object System.Windows.Forms.Label

$label.Text = "Enter your name:"

$label.Location = New-Object System.Drawing.Point(10, 20)

$label.Size = New-Object System.Drawing.Size(100, 20)

$form.Controls.Add($label)


# Create a text box

$textBox = New-Object System.Windows.Forms.TextBox

$textBox.Location = New-Object System.Drawing.Point(120, 20)

$textBox.Size = New-Object System.Drawing.Size(150, 20)

$form.Controls.Add($textBox)


# Create a button

$button = New-Object System.Windows.Forms.Button

$button.Text = "Submit"

$button.Location = New-Object System.Drawing.Point(10, 60)

$button.Size = New-Object System.Drawing.Size(75, 30)

$form.Controls.Add($button)


# Add button click event

$button.Add_Click({

    [System.Windows.Forms.MessageBox]::Show("Hello, $($textBox.Text)!")

})


# Show the form

$form.Add_Shown({ $form.Activate() })

[void]$form.ShowDialog()


Key Points:

  1. Assembly: Use Add-Type to load System.Windows.Forms.
  2. Controls: Add controls like labels, textboxes, and buttons using New-Object.
  3. Events: Use Add_Click to define actions when buttons are clicked.

Running the Script:

  • Save the script as MyForm.ps1.
  • Run it in PowerShell (powershell.exe or pwsh).

For more advanced UI, consider using WPF with XAML for a richer user interface. Let me know if you need an example using WPF!

Comments

Popular posts from this blog

Ag Grid Angular

.NET Watcher in a .NET Core API