这个特定的powershell代码对我不起作用,我想知道我在这里做错了什么(我正在学习Powershell-我对此很陌生)
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()
当“ $ SetCheckBox.Checked”等于$ true“时,我希望它向我的表单添加一个按钮,但是什么也没有发生。 也许这不是Powershell的工作方式?
答案 0 :(得分:1)
问题在于您没有正确设置所有设置。您需要在按钮按下中包含if语句,然后对其进行操作。我不需要放入Refresh()
,但只在ISE中进行了测试。目前正在为我工作:
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()