have to close and re-open form, for it to work again

This will check if 7-zip & Notepad++ are installed, and if they are selected or un-selected, it will install or un-install them.

My problem is that, if for example I select 7-zip and install it, if I then un-select it and click OK, it will not un-install it.

I have to close the form, re-open it, and then if I un-select 7-zip and click OK it will un-install it.

 

It’s like, after each operation, the form must be closed and then re-opened, for it to work again.

Is there a way I can avoid this?

Thanks

 

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$software1 = "7-Zip"
$software2 = "Notepad++"

$software1status = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where {$_.DisplayName -like "*$software1*"}
$software2status = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where {$_.DisplayName -like "*$software2*"}

# set form size
$Form = New-Object System.Windows.Forms.Form
$Form.width = 500
$Form.height = 500
$Form.Text = 'Install Software'

# set font
$Font = New-Object System.Drawing.Font("Verdana",10)
$Form.Font = $Font

# checkbox software1
$checkbox1 = new-object System.Windows.Forms.checkbox
$checkbox1.Location = new-object System.Drawing.Size(30,30)
$checkbox1.Size = new-object System.Drawing.Size(120,20)
$checkbox1.Text = "$software1"
if ($software1status -eq $null) {$checkbox1.Checked = $false} Else {$checkbox1.Checked = $true}
$Form.Controls.Add($checkbox1)

# checkbox software2
$checkbox2 = new-object System.Windows.Forms.checkbox
$checkbox2.Location = new-object System.Drawing.Size(30,50)
$checkbox2.Size = new-object System.Drawing.Size(120,20)
$checkbox2.Text = "$software2"
if ($software2status -eq $null) {$checkbox2.Checked = $false} Else {$checkbox2.Checked = $true}
$Form.Controls.Add($checkbox2)

# ok button
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = new-object System.Drawing.Size(130,400)
$OKButton.Size = new-object System.Drawing.Size(100,40)
$OKButton.Text = "OK"
$form.Controls.Add($OKButton)

# cancel button
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = new-object System.Drawing.Size(255,400)
$CancelButton.Size = new-object System.Drawing.Size(100,40)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$Form.Close()})
$form.Controls.Add($CancelButton)


$OKButton.Add_Click{

if($checkbox1.Checked -and $software1status -eq $null) {Start-Process -FilePath $PSScriptRoot\software\7z1900-x64.exe /S}
if($checkbox1.Checked -eq $false -and $software1status -ne $null ) {Start-Process -FilePath "C:\Program Files\7-Zip\Uninstall.exe" /S}

if($checkbox2.Checked -and $software2status -eq $null) {Start-Process -FilePath $PSScriptRoot\software\npp.7.8.1.Installer.x64.exe /S}
if($checkbox2.Checked -eq $false -and $software2status -ne $null ) {Start-Process -FilePath "C:\Program Files\Notepad++\uninstall.exe" /S}

}

# activate form
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

I am not an expert on windows forms, but it sounds like you are not refreshing the GUI with new information. Possibly make a function to refresh the data displayed on the screen.

How do I do that?