0

I created a form with checkboxes and wanted the script to execute the lines if the checkboxes are selected and I press OK. But for some reason it's not opening. I used both $checkbox1 -eq $true and $checkbox1.checked.

Please take a look at my script, is it place it in the wrong spot? (The last part is the code after you press OK)

Script :

$formPrinterInstallerProg = New-Object 'System.Windows.Forms.Form'
$checkbox2 = New-Object 'System.Windows.Forms.CheckBox'
$checkbox1 = New-Object 'System.Windows.Forms.CheckBox'
$label2 = New-Object 'System.Windows.Forms.Label'
$label1 = New-Object 'System.Windows.Forms.Label'
$Label = New-Object 'System.Windows.Forms.Label'
$buttonOK = New-Object 'System.Windows.Forms.Button'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState' 

$formPrinterInstallerProg.Controls.Add($checkbox2)
$formPrinterInstallerProg.Controls.Add($checkbox1)
$formPrinterInstallerProg.Controls.Add($label2)
$formPrinterInstallerProg.Controls.Add($label1)
$formPrinterInstallerProg.Controls.Add($Label)
$formPrinterInstallerProg.Controls.Add($buttonOK)
$formPrinterInstallerProg.AcceptButton = $buttonOK
$formPrinterInstallerProg.AutoScaleDimensions = '6, 13'
$formPrinterInstallerProg.AutoScaleMode = 'Font'
$formPrinterInstallerProg.ClientSize = '545, 362'
$formPrinterInstallerProg.FormBorderStyle = 'FixedDialog'
$formPrinterInstallerProg.MaximizeBox = $False
$formPrinterInstallerProg.MinimizeBox = $False
$formPrinterInstallerProg.Name = 'formPrinterInstallerProg'
$formPrinterInstallerProg.StartPosition = 'CenterScreen'
$formPrinterInstallerProg.Text = 'Printer Installer Program'
$formPrinterInstallerProg.add_Load($formPrinterInstallerProg_Load)
#
# checkbox2
#
$checkbox2.Location = '12, 101'
$checkbox2.Name = 'checkbox2'
$checkbox2.Size = '104, 24'
$checkbox2.TabIndex = 5
$checkbox2.Text = 'checkbox2'
$checkbox2.UseCompatibleTextRendering = $True
$checkbox2.UseVisualStyleBackColor = $True
$checkbox2.add_CheckedChanged($checkbox2_CheckedChanged)
#
# checkbox1
#
$checkbox1.Location = '13, 71'
$checkbox1.Name = 'checkbox1'
$checkbox1.Size = '104, 24'
$checkbox1.TabIndex = 4
$checkbox1.Text = 'checkbox1'
$checkbox1.UseCompatibleTextRendering = $True
$checkbox1.UseVisualStyleBackColor = $True
$checkbox1.add_CheckedChanged($checkbox1_CheckedChanged)
#
# label2
#
$label2.AutoSize = $True
$label2.Font = 'Microsoft Sans Serif, 11pt'
$label2.Location = '12, 27'
$label2.Name = 'label2'
$label2.Size = '46, 22'
$label2.TabIndex = 3
$label2.Text = 'label2'
$label2.UseCompatibleTextRendering = $True
#
# label1
#
$label1.AutoSize = $True
$label1.Font = 'Microsoft Sans Serif, 11pt'
$label1.Location = '12, 327'
$label1.Name = 'label1'
$label1.Size = '46, 22'
$label1.TabIndex = 2
$label1.Text = 'label1'
$label1.UseCompatibleTextRendering = $True
#
# Label
#
$Label.Font = 'Microsoft Sans Serif, 11pt'
$Label.Location = '12, 9'
$Label.Name = 'Label'
$Label.Size = '320, 18'
$Label.TabIndex = 1
$Label.Text = 'label'
$Label.UseCompatibleTextRendering = $True
$Label.add_Click($Label_Click)
#
# buttonOK
#
$buttonOK.Anchor = 'Bottom, Right'
$buttonOK.DialogResult = 'OK'
$buttonOK.Location = '458, 327'
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = '75, 23'
$buttonOK.TabIndex = 0
$buttonOK.Text = '&OK'
$buttonOK.UseCompatibleTextRendering = $True
$buttonOK.UseVisualStyleBackColor = $True
$formPrinterInstallerProg.ResumeLayout()


$buttonOK_Click = {
    if($checkbox1 -eq $True) {
        Write-Host 'Checked'
        Start-Process C:\windows\System32\notepad.exe   
    }
}
Show-New-Form_psf | Out-Null

1 Answer 1

2

There is no click event attached to button and there is no function $checkbox1_CheckedChanged. See following working demo:

$formPrinterInstallerProg = New-Object 'System.Windows.Forms.Form'
$checkbox1 = New-Object 'System.Windows.Forms.CheckBox'
$buttonOK = New-Object 'System.Windows.Forms.Button'

$formPrinterInstallerProg.Controls.Add($checkbox1)
$formPrinterInstallerProg.Controls.Add($buttonOK)
$formPrinterInstallerProg.AcceptButton = $buttonOK
$formPrinterInstallerProg.AutoScaleDimensions = '6, 13'
$formPrinterInstallerProg.AutoScaleMode = 'Font'
$formPrinterInstallerProg.ClientSize = '545, 362'
$formPrinterInstallerProg.FormBorderStyle = 'FixedDialog'
$formPrinterInstallerProg.MaximizeBox = $False
$formPrinterInstallerProg.MinimizeBox = $False
$formPrinterInstallerProg.Name = 'formPrinterInstallerProg'
$formPrinterInstallerProg.StartPosition = 'CenterScreen'
$formPrinterInstallerProg.Text = 'Printer Installer Program'
#
# checkbox1
#
$checkbox1.Location = '13, 71'
$checkbox1.Name = 'checkbox1'
$checkbox1.Size = '104, 24'
$checkbox1.TabIndex = 4
$checkbox1.Text = 'checkbox1'
$checkbox1.UseCompatibleTextRendering = $True
$checkbox1.UseVisualStyleBackColor = $True
$checkbox1.add_CheckedChanged({
    if ($checkbox1.Checked){
        [System.Windows.Forms.MessageBox]::Show("I'm checked now!", "My Dialog Box")
    }
})
#
# buttonOK
#
$buttonOK.Anchor = 'Bottom, Right'
$buttonOK.DialogResult = 'OK'
$buttonOK.Location = '458, 327'
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = '75, 23'
$buttonOK.TabIndex = 0
$buttonOK.Text = '&OK'
$buttonOK.UseCompatibleTextRendering = $True
$buttonOK.UseVisualStyleBackColor = $True
$buttonOk.add_Click({
    if ($checkbox1.Checked){
        [System.Windows.Forms.MessageBox]::Show("I'm checked!", "My Dialog Box")
    }
})
$formPrinterInstallerProg.ResumeLayout()

$formPrinterInstallerProg.ShowDialog() | Out-Null

Hint: make sure your example is minimal - you need only button and checkbox to show your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I forgot about click event into the form, I thought just calling the variable out of no where would work.

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.