2017-10-18 166 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 
相關問題