2017-10-18 136 views
0

我试图执行exit单击“否”按钮时,请关闭后的形式,但exit导致系统错误出口导致未处理的异常

代码:

function No { 
      $Form.close() 
      exit 
    } 
$NoButton.Add_Click({No}) 

没有退出,它关闭的形式,但它继续执行脚本

系统错误:

Unhandled exception has occured in a component in your application. If you click Continue, the application will ignore this error and attempt to continue.

System error.

全部按钮代码:

function No { 
    $Form.close() 
    exit 
        } #end No 
# No button 
$NoButton = New-Object System.Windows.Forms.Button 
$NoButton.Location = New-Object System.Drawing.Size(95,80) 
$NoButton.Size = New-Object System.Drawing.Size(80,20) 
$NoButton.Text = "No" 
$NoButton.Add_Click({No}) 
$NoButton.Cursor = [System.Windows.Forms.Cursors]::Hand 
$Form.Controls.Add($NoButton) 
+0

使用'[系统时,我无法重现此错误。 Windows.Forms.Application] :: Run($ Form)'来显示窗口 - 你能展示代码来证明这个问题吗? –

回答

0

您需要等到$Form实际上被关闭,然后最好杀了它自己的过程:

$NoButton = New-Object System.Windows.Forms.Button 
$NoButton.Location = New-Object System.Drawing.Size(95,80) 
$NoButton.Size = New-Object System.Drawing.Size(80,20) 
$NoButton.Text = "No" 
$NoButton.Add_Click({$Form.close()}) 
$NoButton.Cursor = [System.Windows.Forms.Cursors]::Hand 
$Form.Controls.Add($NoButton) 
$Form.Add_Closed({Stop-Process -ID $PID}) # Kill it's own process when the form is closed 
相关问题