Cannot call show dialog() on a null-valued expression - powershell

I am trying to open the next page of my Winforms script and atm on first launch the error is get when clicking the next button is "you cannot call a method on a null-valued expression" [void] $Form_UserInformation.ShowDialog() being the issue code here. When I run it the second time it runs fine but this doesn't work for an EXE file as it closes it and doesn't remember the previous session.
# HomePage Form
$Form_HomePage = New-Object System.Windows.Forms.Form
$Form_HomePage.text = "New User Script"
$Form_HomePage.Size = New-Object System.Drawing.Size(400,400)
$Form_HomePage.FormBorderStyle = "FixedDialog"
$Form_HomePage.TopMost = $false
$Form_HomePage.MaximizeBox = $false
$Form_HomePage.MinimizeBox = $true
$Form_HomePage.ControlBox = "true"
$Form_HomePage.StartPosition = "CenterScreen"
$Form_HomePage.Font = "Segoe UI"
# Title label
$label_HomeTitle = New-Object System.Windows.Forms.Label
$label_HomeTitle.Location = New-Object System.Drawing.Size(47,8)
$label_HomeTitle.Size = New-Object System.Drawing.Size(300,42)
$label_HomeTitle.Font = New-Object System.Drawing.Font("Segoe UI",24,[System.Drawing.FontStyle]::Regular)
$label_HomeTitle.TextAlign = "MiddleCenter"
$label_HomeTitle.Text = "Kuehne && Nagel"
$Form_HomePage.Controls.Add($label_HomeTitle)
# Subheading label
$label_HomeSubTitle = New-Object System.Windows.Forms.Label
$label_HomeSubTitle.Location = New-Object System.Drawing.Size(50,60)
$label_HomeSubTitle.Size = New-Object System.Drawing.Size(300,42)
$label_HomeSubTitle.Font = New-Object System.Drawing.Font("Segoe UI",16,[System.Drawing.FontStyle]::Regular)
$label_HomeSubTitle.TextAlign = "MiddleCenter"
$label_HomeSubTitle.Text = "New User Script"
$Form_HomePage.Controls.Add($label_HomeSubTitle)
# Next button
$button_HomeStart = New-Object System.Windows.Forms.Button
$button_HomeStart.Location = New-Object System.Drawing.Size(75,200)
$button_HomeStart.Size = New-Object System.Drawing.Size(240,32)
$button_HomeStart.TextAlign = "MiddleCenter"
$button_HomeStart.Text = "Start"
$button_HomeStart.Add_Click({
[void] $Form_UserInformation.ShowDialog()
$Form_HomePage.Hide()
})
$Form_HomePage.Controls.Add($button_HomeStart)
# About button
$button_About = New-Object System.Windows.Forms.Button
$button_About.Location = New-Object System.Drawing.Size(75,250)
$button_About.Size = New-Object System.Drawing.Size(240,32)
$button_About.TextAlign = "MiddleCenter"
$button_About.Text = "About"
$button_About.Add_Click({
})
$Form_HomePage.Controls.Add($button_About)
# Exit button
$button_HomeExit = New-Object System.Windows.Forms.Button
$button_HomeExit.Location = New-Object System.Drawing.Size(75,300)
$button_HomeExit.Size = New-Object System.Drawing.Size(240,32)
$button_HomeExit.TextAlign = "MiddleCenter"
$button_HomeExit.Text = "Exit"
$button_HomeExit.Add_Click({
$Form_HomePage.Close()
})
$Form_HomePage.Controls.Add($button_HomeExit)
# Show Form
$Form_HomePage.Add_Shown({$Form_HomePage.Activate()})
[void] $Form_HomePage.ShowDialog()
# User Information Form
$Form_UserInformation = New-Object System.Windows.Forms.Form
$Form_UserInformation.text = "New User Script"
$Form_UserInformation.Size = New-Object System.Drawing.Size(400,400)
$Form_UserInformation.FormBorderStyle = "FixedDialog"
$Form_UserInformation.TopMost = $false
$Form_UserInformation.MaximizeBox = $false
$Form_UserInformation.MinimizeBox = $true
$Form_UserInformation.ControlBox = "true"
$Form_UserInformation.StartPosition = "CenterScreen"
$Form_UserInformation.Font = "Segoe UI"

Related

System.Windows.Forms.TabPage - add_LostFocus and add_GotFocus not working?

I have a form with multiple TabPages. I have buttons that I want to only be visible when one tab is in focus. Here's my code:
$form = New-Object system.Windows.Forms.Form
$form.ClientSize = New-Object System.Drawing.Point(350,380)
$form.text = "Form"
$tabcontrol = New-object System.Windows.Forms.TabControl
$tabcontrol.Size = New-Object System.Drawing.Point(330,330)
$tabcontrol.Location = New-Object System.Drawing.Point(10,10)
$form.Controls.Add($tabcontrol)
$tab0 = New-object System.Windows.Forms.Tabpage
$tab0.DataBindings.DefaultDataSourceUpdateMode = 0
$tab0.UseVisualStyleBackColor = $True
$tab0.Text = "Tab0"
$tab0.AutoScroll = $true
$tabcontrol.Controls.Add($tab0)
$add_button = New-Object System.Windows.Forms.Button
$add_button.Size = New-Object System.Drawing.Size(23,23)
$add_button.Location = New-Object System.Drawing.Point(10,350)
$add_button.Text = "+"
$form.Controls.Add($add_button)
$remove_button = New-Object System.Windows.Forms.Button
$remove_button.Size = New-Object System.Drawing.Size(23,23)
$remove_button.Location = New-Object System.Drawing.Point(43,350)
$remove_button.Text = "-"
$form.Controls.Add($remove_button)
$tab0.add_lostFocus({
$add_button.Hide()
$remove_button.Hide()
})
$tab0.add_gotFocus({
$add_button.Show()
$remove_button.Show()
})
$tab1 = New-object System.Windows.Forms.Tabpage
$tab1.DataBindings.DefaultDataSourceUpdateMode = 0
$tab1.UseVisualStyleBackColor = $True
$tab1.Text = "Tab1"
$tabcontrol.Controls.Add($tab1)
Despite the add_GotFocus, and add_LostFocus on $tab0, when I change between tabs, the buttons stay visible.
What am I doing wrong and how can I accomplish this?
I believe the events you're looking for are Enter and Leave:
$tab0.Add_Leave({
$add_button, $remove_button | ForEach-Object Hide
})
$tab0.Add_Enter({
$add_button, $remove_button | ForEach-Object Show
})

PowerShell ISE GUI how to live monitor and changed the jpg based on variable

I would like to ask how to refresh and changed the jpg below GUI from "Offline" to "Online" based on the live monitoring of specific folder and filenames. For example, I am monitoring a folder for any changes, if in the folder a filename exists with specific string, then it will shows in GUI as "Online" and when the folder doesn't contain the file with specific string, then it will shows as "Offline" in the GUI.
Below is the code that I have.
####################################### Form settings ##############################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$Form.Anchor = "Top,Bottom,Left,Right"
$Form.Size = New-Object System.Drawing.Size(1920,1600)
$Form.AutoScale = $True
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $True
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Auto"
$Form.AutoSizeMode = New-Object System.Windows.Forms.AutoSizeMode
$Form.SizeGripStyle = "Show"
$Form.BackColor = "LightGray"
$Form.Icon = $Icon
$TestStatus16 = Test-Path -Path C:\Desktop\Newfolder\P16*
$Online = [System.Drawing.Image]::FromFile("C:\Online.jpg")
$Offline = [System.Drawing.Image]::FromFile("C:\Offline.jpg")
$Photo16 = New-Object System.Windows.Forms.Button
$Photo16.Location = New-Object System.Drawing.Point(830,250)
$Photo16.Size = New-Object System.Drawing.Size(200,50)
$Photo16.Font = New-Object System.Drawing.Font ("HelveticaNeueLTStd-Roman",11,[System.Drawing.FontStyle]::Bold)
$Photo16.Text = "Photo # 16"
$Photo16.BackColor = "SandyBrown"
$Photo16.ForeColor = "Black"
$Form.Controls.Add($Photo16)
$StatusPhoto16 = New-Object System.Windows.Forms.PictureBox
$StatusPhoto16.Location = New-Object System.Drawing.Size(1050,252)
$StatusPhoto16.Size = New-Object System.Drawing.Size(150,45)
$StatusPhoto16.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
$StatusPhoto16.BackColor = "White"
$Form.Controls.Add($StatusPhoto16)
if($TestStatus16 -eq $True){$StatusPhoto16.Image=$Online}else{$StatusPhoto16.Image=$Offline}
$Timer16 = New-Object System.Windows.Forms.Timer
$Timer16.Interval = 3000
$Timer16.Add_Tick({Status16 $StatusPhoto16})
$Timer16.Enabled = $False
$Timer16.Start()
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
Kindly help improving the above code.
You're missing is what I'm assuming it to be the function call for your Test-Path.
What's needed is for it to be defined, the object ($StatusPhoto16) be passed to it, and the update of the actual image based on your Test-Path.
####################################### Form settings ##############################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$Form.Anchor = "Top,Bottom,Left,Right"
$Form.Size = New-Object System.Drawing.Size(1920,1600)
$Form.AutoScale = $True
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $True
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Auto"
$Form.AutoSizeMode = New-Object System.Windows.Forms.AutoSizeMode
$Form.SizeGripStyle = "Show"
$Form.BackColor = "LightGray"
$Form.Icon = $Icon
$Online = [System.Drawing.Image]::FromFile("C:\Online.jpg")
$Offline = [System.Drawing.Image]::FromFile("C:\Offline.jpg")
$Photo16 = New-Object System.Windows.Forms.Button
$Photo16.Location = New-Object System.Drawing.Point(830,250)
$Photo16.Size = New-Object System.Drawing.Size(200,50)
$Photo16.Font = New-Object System.Drawing.Font ("HelveticaNeueLTStd-Roman",11,[System.Drawing.FontStyle]::Bold)
$Photo16.Text = "Photo # 16"
$Photo16.BackColor = "SandyBrown"
$Photo16.ForeColor = "Black"
$Form.Controls.Add($Photo16)
function status16 ($StatusPhoto16) {
if (Test-Path -Path 'C:\Desktop\Newfolder\P16*')
{
$StatusPhoto16.Image = $Online
}
else
{
$StatusPhoto16.Image = $Offline
}
}
$StatusPhoto16 = New-Object System.Windows.Forms.PictureBox
$StatusPhoto16.Location = New-Object System.Drawing.Size(1050,252)
$StatusPhoto16.Size = New-Object System.Drawing.Size(150,45)
$StatusPhoto16.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
$StatusPhoto16.BackColor = "White"
$Form.Controls.Add($StatusPhoto16)
$Timer16 = New-Object System.Windows.Forms.Timer
$Timer16.Interval = 3000
$Timer16.Add_Tick({status16 $StatusPhoto16})
$Timer16.Enabled = $False
$Timer16.Start()
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

PowerShell GUI - Function within a Form not working

currently i am working on an little PowerShell GUI Porject.
I have a Form within this Form there's a Button to call another Form that provides an Listbox with some items in it.
So far so good - i'm getting the results as expected.
Now i mark one of the items - click on OK - then it should do something.
And here is my Problem, it does nothing :D No Error, no warning, just nothing.
If i test with write-host "$($listBox.SelectedItem)" - i get the selected Item as i expect it.
But if you want to use it with get-adfsrelyingpartytrust -Name $listBox.SelectedItem - nothing happens.
If i execute the function without the rest (so that only the listbox appears) everything works as intended O_o.
Code:
function single_rpt{ $RPTForm = New-Object System.Windows.Forms.Form
$RPTForm.ClientSize = '400,200'
$RPTForm.Text = "Choose"
$RPTForm.BackColor = "#b2b2b2"
$RPTForm.AutoSize = $false
$RPTForm.StartPosition = 'CenterScreen'
$RPTForm.ControlBox = $true
$rptButton = New-Object System.Windows.Forms.Button
$rptButton.BackColor = "#ffffff"
$rptButton.Text = "OK"
$rptButton.Width = 90
$rptButton.Height = 30
$rptButton.Location = New-Object System.Drawing.Point(20,125)
$rptButton.Font = 'Microsoft Sans Serif,10'
$rptButton.ForeColor = "#000000"
$rptButton.UseVisualStyleBackColor = $true
$rptButton.DialogResult = 1
# Test List Box
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(20,50)
$listBox.Size = New-Object System.Drawing.Size(350,200)
$listBox.Height = 80
$RPTForm.Controls.AddRange(#($listBox,$rptButton))
$listboxArray = #()
$ADFS_RPTS = Get-AdfsRelyingPartyTrust
Foreach($rpt in $ADFS_RPTS)
{
#$Object = Add-Member -TypeName NoteProperty -Name Service -Value $service.Name
$listboxArray += $rpt.Name
}
$listBox.Items.AddRange($listboxArray)
[void]$RPTForm.ShowDialog()
if($RPTForm.DialogResult -eq "OK")
{
Get-AdfsReylingPartyTrus -Name $listBox.SelectedItem
}
}
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create Form with size, title and background color, etc.
$TESTForm = New-Object System.Windows.Forms.Form
$TESTForm.ClientSize = '250,300'
$TESTForm.Text = "TEST"
$TESTForm.BackColor = "#ffffff"
$TESTForm.AutoSize = $false
$TESTForm.StartPosition = 'CenterScreen'
$TESTForm.ControlBox = $true
$RPTSingle = New-Object System.Windows.Forms.Button
$RPTSingle.BackColor = "#ffffff"
$RPTSingle.Text = "RPTSingle"
$RPTSingle.Width = 90
$RPTSingle.Height = 30
$RPTSingle.Location = New-Object System.Drawing.Point(75,125)
$RPTSingle.Font = 'Microsoft Sans Serif,10'
$RPTSingle.ForeColor = "#000000"
$RPTSingle.UseVisualStyleBackColor = $true
$RPTSingle.Add_Click({single_rpt})
# Add a Cancel Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.BackColor = "#ffffff"
$CancelButton.Text = "Cancel"
$CancelButton.Width = 90
$CancelButton.Height = 30
$CancelButton.Location = New-Object System.Drawing.Point(75,250)
$CancelButton.Font = 'Microsoft Sans Serif,10'
$CancelButton.ForeColor = "#000000"
$CancelButton.UseVisualStyleBackColor = $true
$CancelButton.DialogResult = 2
$TESTForm.Controls.AddRange(#($RPTSingle,$CancelButton))
# Display the form
[void]$TESTForm.ShowDialog()
#------------------------------------#

ComboBox.SelectedItem giving Null value

I have been stuck in this for whole day. I am creating a UI using PowerShell forms. In that:
user select an option from 1st combobox. click button Go
Based on the selection, a panel will appear having another combobox.
if user select another option in 1st combobox then another panel appears with another combobox
After selecting options from panel comboboxes, user clicks on start button.
This leads to a function which stores the selected options to a variable.
Problem
Now when user selects the options from the comboboxes of panels, I am using $combobox.selecteditem.Tostring to get the values.
But it gives me NULL result.
Here is my code..
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.Add("View")
$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
######### code Starts ###########
function Button1_Click()
{
$link = $comboBox2.SelectedItem.ToString();
$linktype = $comboBox3.SelectedItem.ToString();
}
####### end of code ######
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
The issue is that your variables are being set in a function, so they are scoped to that function. There's really no reason to put that in a function, put it directly in the .add_click() instead. Or if you feel that you need to keep it in the function you can scope the variable like you did with $global:ButtonClicked and set them to $global:Link and $global:LinkType.
Edit: Order does make a difference in PowerShell, so I did move some stuff around in your script to make it work right, but I was able to get values to reflect fine when I put them in the global scope.
$global:Button1Clicked = 0;
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.FormClosingEventHandler]
######### code Starts ###########
function Button1_Click()
{
write-host "Link = $global:link"
write-host "Link Type = $global:linktype"
}
Function Button2_Click()
{
if ($ComboBox1.SelectedIndex -eq 0)
{
$panel2.Visible = $true
$panel1.Visible = $false
}
if ($ComboBox1.SelectedIndex -eq 1)
{
$panel1.Visible = $true
$panel2.Visible = $false
}
}
####### end of code ######
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '1500,800'
$Form.text = "NextUI "
$Form.BackColor = 'White'
$Form.TopMost = $true
$Form.add_closing(
{
if($global:Button2Clicked)
{
$global:Button2Clicked = 0;
$_.cancel = $true;
}
})
################ Module combo box ################
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = "Select Module"
$ComboBox1.width = 200
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(310, 45)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$combobox1.items.Add("ControlSettings")
$combobox1.items.Add("NextSettings")
######## file and folder permission #############
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = "select an option"
$ComboBox2.width = 200
$ComboBox2.height = 20
$ComboBox2.location = New-Object System.Drawing.Point(450, 200)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$ComboBox2.items.AddRange(#("View","Edit") )
#$ComboBox2.items.Add("Edit")
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = "select an option"
$ComboBox3.width = 200
$ComboBox3.height = 20
$ComboBox3.location = New-Object System.Drawing.Point(450, 200)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$ComboBox3.items.Add("View")
$ComboBox3.items.Add("Edit")
$panel1 = New-Object system.Windows.Forms.Panel
$panel1.AutoSize = $true
$panel1.Width = 1200
$panel1.Height = 200
$panel1.location = New-Object System.Drawing.Point(50,100)
$panel1.Visible = $false
$panel1.Controls.Add($label3)
$panel1.Controls.Add($comboBox2)
$panel1.BorderStyle = 1
$panel2 = New-Object system.Windows.Forms.Panel
$panel2.AutoSize = $true
$panel2.Width = 1200
$panel2.Height =200
$panel2.location = New-Object system.Drawing.Point(50,100)
$panel2.Visible = $false
$panel2.Controls.Add($label4)
$panel2.Controls.Add($ComboBox3)
$panel2.BorderStyle = 1
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Select Module"
$Label1.AutoSize = $true
$Label1.location = New-Object System.Drawing.Point(35,50)
$Label1.Font = 'segoe ui,9.5'
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "SharePoint Settings"
$Label2.AutoSize = $true
$Label2.location = New-Object System.Drawing.Point(35,15)
$Label2.Font = 'Segoe UI Semibold,9.5'
$Label3 = New-Object system.Windows.Forms.Label
$Label3.text = "Choose file and folder permission option"
$Label3.AutoSize = $true
$Label3.location = New-Object System.Drawing.Point(100,200)
$Label3.Font = 'segoe ui,9.5'
$Label4 = New-Object system.Windows.Forms.Label
$Label4.AutoSize = $True
$Label4.text = "File Permission"
$Label4.location = New-Object System.Drawing.Point(100,200)
$Label4.Font = 'Segoe UI ,9.5'
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "Start"
$Button1.width = 150
$Button1.height = 30
$Button1.BackColor = '#F6CEE3'
$Button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button1.location = New-Object System.Drawing.Point(500, 700)
$Button1.Font = 'segoe ui,10'
$Button1.Add_Click({
$global:link = $comboBox2.SelectedItem.ToString();
$global:linktype = $comboBox3.SelectedItem.ToString();
Button1_Click;
$global:Button1Clicked = 1;
})
############# Button 'Go' #############
$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "Go"
$Button2.width = 100
$Button2.height = 30
$Button2.BackColor = '#F6CEE3'
$Button2.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Button2.location = New-Object System.Drawing.Point(680, 43)
$Button2.Font = 'segoe ui,10'
$Button2.Add_Click({
Button2_Click;
$global:Button2Clicked = 1;
})
$form.Controls.Add($Panel1)
$form.Controls.Add($Panel2)
$form.Controls.Add($button1)
$form.Controls.Add($comboBox1)
$form.Controls.Add($button2)
$form.Controls.Add($pictureBox1)
$form.Controls.Add($label1)
$form.Controls.Add($label2)
[void]$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()
When I ran that, set the two combobox values, and clicked Start I got text in the ISE saying what I set the Link and LinkType variables to, and was able to echo $global:link and $global:linktype to see the values correctly assigned.

Problems with Powershell and imageboxes

Trying to create a simple GUI Script using powershell to ping a TP ip address and display it with a simple Green/Red Image depending whether its on/off.
But for some reason, when hitting the Close button, I cant seem to remove the green/red dots.
The File contains:
Arial,172.0.0.0
Bodoni,172.0.0.0
Caslon,172.0.0.1
Script:
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.text = "Hardware Checks"
$Form.Size = New-Object System.Drawing.Size(600,600)
$arrayTPs = Get-Content -path "C:\Activity monitor\TPs.txt"
#Image Locations
$filered = (get-item 'C:\Activity monitor\red1.png')
$filegreen = (get-item 'C:\Activity monitor\green1.png')
$imgred = [System.Drawing.Image]::Fromfile($filered)
$imggreen = [System.Drawing.Image]::Fromfile($filegreen)
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,200)
$outputBox.Size = New-Object System.Drawing.Size(565,200)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$Form.Controls.Add($outputBox)
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(450,30)
$Button.Size = New-Object System.Drawing.Size(75,75)
$Button.Text = "Check"
$Button.Add_Click({
$Form.controls.Remove($outputBox)
$Form.controls.refresh()
foreach($room in $arrayTPs){
#Get Name of room from array
$roomAlone = $room -split ","
$roomAlone[0]
$pictureBoxRed = new-object Windows.Forms.PictureBox
$pictureBoxGreen = new-object Windows.Forms.PictureBox
#Add room name to textbox
if($roomAlone[1] -eq "172.0.0.0"){
$pictureBoxGreen.Width = 10
$pictureBoxGreen.Height = 10
$pictureBoxGreen.Image = $imggreen
$pictureBoxGreen.Location = New-Object System.Drawing.Point(90,$counterpic)
$outputBox.controls.add($pictureBoxGreen)
$Form.Controls.Add($outputBox)}
else{
$pictureBoxRed.Width = 10
$pictureBoxRed.Height = 10
$pictureBoxRed.Image = $imgred
$pictureBoxRed.Location = New-Object System.Drawing.Point(90,$counterpic)
$outputBox.controls.add($pictureBoxRed)
$Form.Controls.Add($outputBox)
}
$counterpic = $Counterpic + 20
$counter = $Counter + 20
}})
$Form.Controls.Add($Button)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(450,520)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Close"
$cancelButton.Add_Click({
$outputBox.controls.Remove($pictureBoxRed)
$outputBox.controls.Remove($pictureBoxGreen)
$outputBox.Controls.Equals($null)
$outputBox.controls.update()
$Form.Refresh()
foreach($room in $arrayTPs){
#Get Name of room from array
$roomAlone = $room -split ","
$roomAlone[0]
$pictureBoxRed = new-object Windows.Forms.PictureBox
$pictureBoxGreen = new-object Windows.Forms.PictureBox
}})
$Form.Controls.Add($cancelButton)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()