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

时间:2015-01-13 11:59:01

标签: .net winforms forms powershell

点击ButtonOk并选择任何内容时,系统会向用户显示弹出窗口。如果他点击,表单和弹出窗口需要关闭,这部分工作正常。但是,如果他点击,则只需关闭弹出窗口,表单就会再次显示。这最后一步似乎无法弄清楚。

我在$form.Visible = $true内使用$ButtonOK_Click进行了尝试,但这并没有做任何事情。

代码:

$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()

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

2 个答案:

答案 0 :(得分:1)

问题在于您的$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'}
        }
    }
}

答案 1 :(得分:-1)

$MainWindowTitle = "pop-up-name-here"
Get-Process | Where-Object {$_.MainWindowTitle -eq $MainWindowTitle} | Stop-Process -Force