Powershell Winform。如何隐藏或禁用组合框直到选中 chekbox?

Powershell Winform。如何隐藏或禁用组合框直到选中 chekbox?

我尝试在“if CheckBox3.Checked”下使用Visible 和 Enabled,但是它不起作用 =( 如何使其工作?

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
    
$Form = New-Object System.Windows.Forms.Form
$Form.AutoSize = $true
    
$CheckBox1 = New-Object System.Windows.Forms.CheckBox
$CheckBox1.Text = '1'
$CheckBox1.AutoSize = $true
$CheckBox1.Checked = $true
$CheckBox1.Location  = New-Object System.Drawing.Point(10,10)
$Form.Controls.Add($CheckBox1)
    
$CheckBox2 = New-Object System.Windows.Forms.CheckBox
$CheckBox2.Text = '2'
$CheckBox2.AutoSize = $true
$CheckBox2.Checked = $true
$CheckBox2.Location  = New-Object System.Drawing.Point(10,40)
$Form.Controls.Add($CheckBox2)
    
$CheckBox3 = New-Object System.Windows.Forms.CheckBox
$CheckBox3.Text = '3'
$CheckBox3.AutoSize = $true
$CheckBox3.Checked = $false
$CheckBox3.Location  = New-Object System.Drawing.Point(10,70)
$Form.Controls.Add($CheckBox3)
    
$FormComboBox = New-Object System.Windows.Forms.ComboBox
$FormComboBox.Enabled = $false
# OR
# $FormComboBox.Visible = $false

$FormComboBox.Text = "choise:"
$FormComboBox.Width = 120
$FormComboBox.Location = New-Object System.Drawing.Point(70,70)
$FormComboBox.Items.Add("item1");
$FormComboBox.Items.Add("item2");
$FormComboBox.Items.Add("item3");
$FormComboBox.selectedIndex = -1 
$Form.Controls.Add($FormComboBox)

if ($CheckBox3.Checked){ $FormComboBox.Enabled = $true  }
# OR
# if ($CheckBox3.Checked){ $FormComboBox.Visible = $true  }

$FormButton = New-Object System.Windows.Forms.Button
$FormButton.Location = New-Object System.Drawing.Point(100,235)
$FormButton.Size = New-Object System.Drawing.Size(120,30)
$FormButton.Text = "Start"
$Form.Controls.Add($FormButton)
    
$FormButton.Add_Click({
        if($checkBox1.Checked){ Write-Host "flag 1" }
        if($checkBox2.Checked){ Write-Host "flag 2" }
        if($checkBox3.Checked){ Write-Host "flag 3" }
        })
    
$Form.ShowDialog() | Out-Null
$Form.Dispose()

答案1

我自己找到了解决方案:

$CheckBox3.Add_CheckStateChanged({
if ($CheckBox3.Checked){ $FormComboBox.Enabled = $true }
else { $FormComboBox.Enabled = $falce }
})

相关内容