Powershell表单文本/文本框一旦不可见就无法再次可见

问题描述 投票:0回答:1

如果取消选中Readme复选框,如何使Text1和Label1重新出现? 如果在自述文件中选中,则 Text1 和 Label1 将不可见。如果随后取消选中自述文件,如何使其再次可见?目前只有一种方法

Clear-Host
[void][system.reflection.assembly]::LoadWithPartialName('System.Drawing')
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
        
$objForm = New-Object System.Windows.Forms.Form

$objForm.Text          = 'Test'
$objForm.Size          = New-Object System.Drawing.Size(500,300)
$objForm.StartPosition = 'CenterScreen'

$CheckedListBox              = New-Object System.Windows.Forms.CheckedListBox
$CheckedListBox.Location     = New-Object System.Drawing.Size(20,20)
$CheckedListBox.size         = New-Object System.Drawing.Size(45,45) 
$CheckedListBox.CheckOnClick = $true 
$CheckedListBox.Items.AddRange(1..1)
$CheckedListBox.ClearSelected()


$CheckBox1              = New-Object System.Windows.Forms.CheckBox
$CheckBox1.Location     = New-Object System.Drawing.Size(20,60)
$CheckBox1.size         = New-Object System.Drawing.Size(80,40)
$CheckBox1.Text         = "Readme"

$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(20,100)
$label1.Size = New-Object System.Drawing.Size(280,20)
$label1.Text = 'Please enter Value in Minutes:'
$label1.Visible = $true

$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(20,120)
$textBox1.Size = New-Object System.Drawing.Size(30,20)
$textBox1.Visible = $true

$CheckBox1.Add_Click({$label1.Visible = $false;$textBox1.Visible = $false })
$CheckBox1.Remove_Click({$label1.Visible = $true;$textBox1.Visible = $true})

$objForm.Controls.Add($CheckedListBox)
$objForm.Controls.Add($CheckBox1)
$objForm.Controls.Add($label1)
$objForm.Controls.Add($TextBox1)

$CheckedListBox.Add_Click({
    For ($i=1;$i -lt $CheckedListBox.Items.count;$i++) 
    {$CheckedListBox}
})


$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,220)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$objForm.AcceptButton = $okButton
$objForm.Controls.Add($okButton)

# put form in front of other windows
$objForm.TopMost = $True

# display the form
$DisplayForm     = $objForm.ShowDialog()

ForEach ($CheckedItem in $CheckedListBox.CheckedItems -join ',')
{
    Switch ($CheckedItem)
    {
        1       {"Checkbox $CheckedItem is checked";Break}
        2       {"Checkbox $CheckedItem is checked";Break}
        3       {"Checkbox $CheckedItem is checked";Break}
        {1,2}   {"Checkbox $CheckedItem is checked";Break}
        {1,3}   {"Checkbox $CheckedItem is checked";Break}
        {2,3}   {"Checkbox $CheckedItem is checked";Break}
        {1..3}  {"Checkbox $CheckedItem is checked";Break}
    }
}

if ($CheckBox1.Checked) {
    Write-Host "Readme is true"
} else {
    Write-Host "Readme is not true"
}

write-host $textBox1.Text.trim()
powershell winforms event-handling
1个回答
0
投票

将您的

.Click
处理程序更改为:

$CheckBox1.Add_Click({
    if ($this.Checked) {
        $label1.Visible = $textBox1.Visible = $false
        return
    }

    $label1.Visible = $textBox1.Visible = $true
})

并删除处理程序:

$CheckBox1.Remove_Click({ $label1.Visible = $true; $textBox1.Visible = $true })
© www.soinside.com 2019 - 2024. All rights reserved.