PowerShell .NET 关闭弹出窗口并返回表单

PowerShell .NET Close pop-up and go back to form

单击 ButtonOk 且未选择任何内容时,会向用户显示一个弹出窗口。如果他点击 Yes,表单和弹出窗口需要关闭,这部分工作正常。但是,如果他单击 No,只需要关闭弹出窗口,表单就会再次可见。最后一步我似乎想不通。

我试过在 $ButtonOK_Click 中使用 $form.Visible = $true,但这并没有真正起到任何作用。

代码:

$script:SelectedOU = $null

Function Get-CheckedNodes {
        Param (
            $nodes
        )
        foreach ($n in $Nodes) {
            if ($n.nodes.count -gt 0) {
                Get-CheckedNodes $n.nodes
            }
            if ($n.checked) {
                Write-Output $n.Text
            }           
        }   
    }

Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '342, 502'
$Form.FormBorderStyle = 'FixedDialog'

$ButtonOK_Click = {
    $script:SelectedOU = Get-CheckedNodes $treeView.Nodes

    if (-not $script:SelectedOU) {
        $Popup = [System.Windows.Forms.MessageBox]::Show(
                "You haven't selected anything.`nAre you sure you want to leave?", 
                "Oops!",'YesNo','Information')
        Switch($Popup) {
            'Yes' {Write-Host 'Yes'}
            'No'  {Write-Host 'No'
                $form.Visible = $true
            }
        }
    }
}

$treeView = New-Object System.Windows.Forms.TreeView
$treeView.Dock = 'Fill'
$treeView.CheckBoxes = $true

$N1 = $treeView.Nodes.Add('Node 1') 
$N2 = $treeView.Nodes.Add('Node 2')
$N3 = $treeView.Nodes.Add('Node 3')

$newNode = New-Object System.Windows.Forms.TreeNode  
$newNode.Name = 'Sub 1'
$newNode.Text = 'Sub 1'
$N1.Nodes.Add($newNode) | Out-Null

$ButtonCancel = New-Object System.Windows.Forms.Button
$ButtonCancel.text = “&Cancel”
$ButtonCancel.Location = '120,467'
$ButtonCancel.size = '75,23'
$ButtonCancel.Anchor = 'Bottom, Left'
$ButtonCancel.add_Click({$form.close()})
$form.Controls.Add($ButtonCancel)

$ButtonOK = New-Object System.Windows.Forms.Button
$ButtonOK.DialogResult = 'OK'
$ButtonOK.Location = '245,467'
$ButtonOK.Size = '75,23'
$ButtonOK.Name = 'ButtonOK'
$ButtonOK.Text = 'OK'
$ButtonOK.add_Click($ButtonOK_Click)
$form.Controls.Add($ButtonOK)
$form.Controls.Add($treeView)
$form.ShowDialog()

一如既往,感谢你们的帮助。

问题出在您的 $ButtonOK 按钮上。您已经分配了一个 DiaglogResult ,我假设它默认关闭表单。如果您在消息框操作中自己处理此问题,则可以避免关闭表单,除非您告诉它。

$ButtonOK 声明更改为:

$ButtonOK = New-Object System.Windows.Forms.Button
$ButtonOK.Location = '245,467'
$ButtonOK.Size = '75,23'
$ButtonOK.Name = 'ButtonOK'
$ButtonOK.Text = 'OK'
$ButtonOK.add_Click($ButtonOK_Click)

并且 $ButtonOK_Click 处理程序:

$ButtonOK_Click = {
    $script:SelectedOU = Get-CheckedNodes $treeView.Nodes

    if (-not $script:SelectedOU) {
        $Popup = [System.Windows.Forms.MessageBox]::Show(
                "You haven't selected anything.`nAre you sure you want to leave?", 
                "Oops!",'YesNo','Information')
        Switch($Popup) {
            'Yes' {
                Write-Host 'Yes'
                $form.close() 
             }
            'No' { Write-Host 'No'}
        }
    }
}
$MainWindowTitle = "pop-up-name-here"
Get-Process | Where-Object {$_.MainWindowTitle -eq $MainWindowTitle} | Stop-Process -Force