我创建了带有复选框的表单,并希望脚本在选中复选框并按OK的情况下执行行。但是由于某种原因,它没有打开。我同时使用了$checkbox1 -eq $true
和$checkbox1.checked
。
请看一下我的脚本,它放置在错误的位置了吗? (最后一部分是按“确定”后的代码)
脚本:
$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
答案 0 :(得分:2)
按钮上没有单击事件,也没有功能$checkbox1_CheckedChanged
。请参阅以下工作演示:
$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
提示:请确保您的示例最少-您只需要按钮和复选框即可显示问题。