I have the following code. The goal is to change text of Label
after pressing the button
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
class MyForm : System.Windows.Forms.Form {
MyForm($mystuff) {
#Do-Stuff
$this.Add_Load( $this.MyForm_Load )
}
$MyForm_Load = {
$mlabel = [System.Windows.Forms.Label]::new()
$mlabel.Name = "status"
$mlabel.Text = "enabled"
$mbutton = [System.Windows.Forms.Button]::new()
$mbutton.Text = "toggle state"
$mbutton.Location = [System.Drawing.Point]::new(100,100)
$mbutton.Add_Click( $this.mbutton_click )
$this.Controls.Add($mlabel)
$this.Controls.Add($mbutton)
}
$mbutton_click = {
if ($this.Parent.Controls["status"].Text -eq "enabled"){
$this.Parent.Controls["status"].Text = "disabled"
}
else{
$this.Parent.Controls["status"].Text = "enabled"
}
}
}
$foo = [MyForm]::new("test")
$foo.ShowDialog()
I am not able run code directly from script due to following error:
At C:\Users\wakatana\oop_vs_proc\forms2_oop.ps1:4 char:16
+ class MyForm : System.Windows.Forms.Form {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.Form].
At C:\Users\wakatana\oop_vs_proc\forms2_oop.ps1:11 char:20
+ $mlabel = [System.Windows.Forms.Label]::new()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.Label].
At C:\Users\wakatana\oop_vs_proc\forms2_oop.ps1:15 char:21
+ $mbutton = [System.Windows.Forms.Button]::new()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Windows.Forms.Button].
At C:\Users\wakatana\oop_vs_proc\forms2_oop.ps1:18 char:30
+ $mbutton.Location = [System.Drawing.Point]::new(100,100)
+ ~~~~~~~~~~~~~~~~~~~~
Unable to find type [System.Drawing.Point].
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : TypeNotFound
The same error is displayed also in VS code and it is underlined by Red. But when I start Powershell console and copy paste first line of the script (Add-Type -AssemblyName System.Windows.Forms
) and then run script from this console then the script run fine. What is the problem?