2015-01-13 52 views
0

單擊ButtonOk並選擇任何內容時,用戶都會看到彈出窗口。如果他點擊是的,窗體和彈出窗口需要關閉,這部分工作正常。但是,如果他點擊,則只有彈出窗口需要關閉,並且窗體應該再次可見。我似乎無法弄清楚這最後一步。PowerShell .NET關閉彈出窗口並返回表單

我試過在$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() 

和往常一樣,感謝您的幫助球員。

回答

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'} 
     } 
    } 
} 
+0

像往常一樣,你是對的,行'$ ButtonOK.DialogResult =「OK''是問題。有一天,我希望能像你一樣聰明,謝謝你! – DarkLite1

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