0

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?

Rizwan
  • 103
  • 4
  • 24
Wakan Tanka
  • 7,542
  • 16
  • 69
  • 122
  • Your “OOP” version uses “$this.Parent”, but your procedural version just uses “Parent” which isn’t a variable name. Do you mean “$global:Form” instead? – mclayton Dec 10 '19 at 00:38
  • @mclayton thank you I've edited original question and make new one https://stackoverflow.com/questions/59261794/winforms-does-not-work-after-rewriting-from-oop-to-procedural-style – Wakan Tanka Dec 10 '19 at 07:00

1 Answers1

0

This is a long standing bug: https://github.com/PowerShell/PowerShell/issues/2074

I posted an example workaround here: System.Windows.Forms.ComboBoxStyle Unable to find type | Powershell

KG-DROID
  • 268
  • 6