PowerShell: Porting from a System.Windows.Forms Form
coding powershell windowsforms dotnet
In the PowerShell tool djaus2/az-iothub-ps on GitHub, I added some graphical UI forms. The UI was first designed in a Windows Forms C# app. This post tells you how.
PowerShell under the hood is .NET. You can invoke .NET APIs directly in PS scripts. You CAN also invoke Windows Forms with some mechanical changes to the Winodws Form designer form. This post documents an example of porting the Form1.Designer.cs file to a PowerShell script.
There are tools for turning a PowerShell script into an .EXE. This port could be construed as step in that direction. Please leave comments below if there is better was of doing this.
Finally, before getting start, I can envisage a tool that parsed a Form.Designer.cs file and automatically ports it to .ps1. … Later.
The PowerShell tool is available on GitHub at djaus2/az-iothub-ps
The end product of this port, show-form.ps1
is here.
Another PS WinFom PS, show-image.ps1
, is here. This displays an image.
And another, a variant of show-image.ps1
, is here. This displays a sequence of images like PPT.
The project ported here is in GitHub here.
The actual file ported to PowerShell, Form1.Designer.cs
can be view here in that project.
Steps for porting a Windows.Forms Form to PowerShell
Start by opening the Form1.Designer.cs
file in Visual Studio.
Layout
In Visual Studio the region Windows Form Designer generated code
is collapsed.
- Copy just that section into a new.ps1 file
- Add the function header and opening brace eg.
function $show-form{
- As the first 2 lines of code below the opening brace, insert:
Add-Type -AssemblyName System.Windows.Forms $Form = New-Object system.Windows.Forms.Form
- Add the closing brace at bottom
At the bottom
- Change
this.ControlsAdd
–>$Form.Controls.Add
- Similarly for all other
this.
in that block change to$Form.
Throughout rest of code:
- Remove all
;
- Change
this.ControlName
to$ControlName
… where ControlName is the various names of controls on the form.
Hint: Can global changethis.
to$
at this point - Replace
new
withNew-Object
- Remove lines with the following control properties
.TabIndex
.TabStop
.Name
Font
Identify common fonts used and instatiate a font object foir each. Reuse those throughout. Enables simple changes
Other
Don’t create any objects inside object instatiation.
- Separately instantiate them first then use them in the second object’s instantiation.
Event Handlers
Change for example:
this.button1.Click += new System.EventHandler(this.button1_Click);
to:
$button1.Add_Click ({ What ever code })
For example:
$button1.Add_Click({
do-whatever
})
And implement the function do-whatever
Lists
Where lists are added eg AddRange for example with Menus, create a range in PowerShell syntax and add that. For example:
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.actionsToolStripMenuItem,
this.editToolStripMenuItem});
Create the list thus:
[System.Windows.Forms.ToolStripItem[]]$items = @($actionsToolStripMenuItem,$editToolStripMenuItem)
The datatype may not be required (??)
Then use thus:
$menuStrip1.Items.AddRange($items)
Topic | Subtopic | |
Next: > | Blazor Gym Booking App | A Booking App for post COVID-19 |
< Prev: | az-iothub-ps | Azure IoT Hub PowerShell V3.0.4 Features |
This Category Links | ||
Category: | Coding Index: | Coding |
Next: > | Nuget Packages 101 | Creating and using a local Nuget Package |
< Prev: | PowerShell | Header Metainfo |