2

This particular powershell code does not work for me, I'm wondering what I'm doing wrong here (I am in the process of learning Powershell - I am quite new to it)

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = "Testing"
$main_form.Width = 500
$main_form.height = 300
$main_form.FormBorderStyle = "FixedDialog"
$main_form.AutoSize = $true
$main_form.MaximizeBox = $false
$main_form.MinimizeBox = $false

#
$SetCheckBox = New-Object System.Windows.Forms.Checkbox
$SetCheckBox.Location = New-Object System.Drawing.Point(10,10)
$SetCheckBox.Size = New-Object System.Drawing.Size(60,20)
$SetCheckBox.Text = "Set"
$main_form.Controls.Add($SetCheckBox)

if($SetCheckBox.Checked -eq $true){
    $SetButton = New-Object System.Windows.Forms.Button
    $SetButton.Location = New-Object System.Drawing.Point(280,200)
    $SetButton.Size = New-Object System.Drawing.Size(100,20)
    $SetButton.Text = "Nothing Selected"
    $SetButton.AutoSize = $true
    $main_form.Controls.Add($SetButton)
}else{

}

$RefreshButton = New-Object System.Windows.Forms.Button
$RefreshButton.Location = New-Object System.Drawing.Point(400,285)
$RefreshButton.Size = New-Object System.Drawing.Size(100,20)
$RefreshButton.Text = "Refresh"
$RefreshButton.AutoSize = $true
$main_form.Controls.Add($RefreshButton)

$RefreshButton.Add_Click({
    $main_form.Refresh()
})
#

$main_form.Topmost = $true
$main_form.ShowDialog()

I expect it to add a button to my form when "$SetCheckBox.Checked" equals to $true", but nothing happens. No error code or anything. Maybe this isn't how Powershell works?

3
  • Please provide enough code for us to test this scenario. We currently don't know how you are calling the snippet or how you WinForms is built :) Commented Sep 30, 2019 at 10:09
  • Assuming your if condition is satisfied, you have to do a $main_form.refresh() for the form changes to be reflected on your screen. Commented Sep 30, 2019 at 10:26
  • @I.TDelinquent Added the full code now :) (The code between the #blocks are the part that doesn't work) Commented Sep 30, 2019 at 10:46

1 Answer 1

2

The problem is that you have everything setup, just not in the right place. You need to include the if statement inside the button press for it to be actioned. I didn't need to put a Refresh() in but I only tested in the ISE. This is currently working for me:

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

$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = "Testing"
$main_form.Width = 500
$main_form.height = 300
$main_form.FormBorderStyle = "FixedDialog"
$main_form.AutoSize = $true
$main_form.MaximizeBox = $false
$main_form.MinimizeBox = $false

$SetCheckBox = New-Object System.Windows.Forms.Checkbox
$SetCheckBox.Location = New-Object System.Drawing.Point(10,10)
$SetCheckBox.Size = New-Object System.Drawing.Size(60,20)
$SetCheckBox.Text = "Set"
$main_form.Controls.Add($SetCheckBox)

$RefreshButton = New-Object System.Windows.Forms.Button
$RefreshButton.Location = New-Object System.Drawing.Point(400,285)
$RefreshButton.Size = New-Object System.Drawing.Size(100,20)
$RefreshButton.Text = "Refresh"
$RefreshButton.AutoSize = $true
$main_form.Controls.Add($RefreshButton)

$RefreshButton.Add_Click({
    if($SetCheckBox.Checked -eq $true){
        Write-Host "testing"
        $SetButton = New-Object System.Windows.Forms.Button
        $SetButton.Location = New-Object System.Drawing.Point(280,200)
        $SetButton.Size = New-Object System.Drawing.Size(100,20)
        $SetButton.Text = "Nothing Selected"
        $SetButton.AutoSize = $true
        $main_form.Controls.Add($SetButton)
    }else{
        Write-Host "testing2"
    }
})

$main_form.Topmost = $true
$main_form.ShowDialog()
Sign up to request clarification or add additional context in comments.

1 Comment

Do not know why my comment got removed, but thank you @i-t-delinquent - that worked perfectly, I appreciate the help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.