locked
Message box comes up behind other Windows RRS feed

  • Question

  • I'm running the following script to select the folder to start removing empty folders from.

    I thought my system was locking up, because if I selected Cancel on the FolderBrowserDialog Window it would sit there and do nothing.  I would have to end the PowerShell task through Task Manager.  After several tries running the script I realize that the MessageBox was coming up behind other Windows.

    Been trying to figure out how to make it come up on top of all other Windows and haven't figured it out.  Could use some help, please.

    [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $browse = New-Object System.Windows.Forms.FolderBrowserDialog
    $browse.SelectedPath = $env:userprofile
    $browse.ShowNewFolderButton = $false
    $browse.Description = "Select a " + $Folder + " folder"
    
    $loop = $true
    while($loop)
      {
        if ($browse.ShowDialog() -eq "OK")
        {
        $loop = $false
    
        Write-Host "Your source is " $browse.SelectedPath #$Folder
    
        } else
        {
          $top = new-Object System.Windows.Forms.Form -property @{Topmost=$true}
          $Result = [System.Windows.Forms.MessageBox]::Show($top,"You clicked Cancel. Would you like to Retry or Cancel?", "Select a location", [System.Windows.Forms.MessageBoxButtons]::RetryCancel)
          if($Result -eq "Cancel")
          {
            #Ends script
            return
          }
        }
      }
    
    $FolderPath = $browse.SelectedPath
    Get-ChildItem -LiteralPath $FolderPath -Force -Recurse | Where-Object {
      $_.PSIsContainer -and `
      @(Get-ChildItem -LiteralPath $_.Fullname -Force -Recurse | Where { -not $_.PSIsContainer }).Count -eq 0 } |
      Remove-Item -Recurse #-WhatIf
    
    $browse.Dispose()
    


    "Fate rarely calls upon us at a moment of our choosing…"

    Thursday, February 6, 2020 7:07 PM

Answers

  • If you need more control over the message box, try using the Visual Basic method:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic") 
    $Result = [Microsoft.VisualBasic.Interaction]::MsgBox("You clicked Cancel. Would you like to Retry or Cancel?", "RetryCancel,SystemModal,Question,DefaultButton1", "Select a location")


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by os_car Friday, February 7, 2020 6:56 PM
    Friday, February 7, 2020 4:50 PM

All replies

  • Your code is just wrong almost from the beginning. 

    It would help to learn PowerShell before trying to guess at how to write a script.  It will save you a lot of wasted time.

    Add-Type -AssemblyName System.Windows.Forms
    
    $browse = New-Object System.Windows.Forms.FolderBrowserDialog
    $browse.SelectedPath = $env:userprofile
    $browse.ShowNewFolderButton = $false
    $browse.Description = "Select a $Folder folder"
    
    while($true){
        if ($browse.ShowDialog() -eq 'OK'){
            Write-Host "Your source is " $browse.SelectedPath
            Get-ChildItem -LiteralPath $browse.SelectedPath -Force -Directory | 
                Remove-Item -Recurse -WhatIf
            break
        }else{
            if('Cancel' -eq [System.Windows.Forms.MessageBox]::Show('You clicked Cancel. Would you like to Retry or Cancel?', 'Select a location','RetryCancel')){
                return
            }
        }
    }
    
    


    \_(ツ)_/


    • Edited by jrv Thursday, February 6, 2020 7:41 PM
    Thursday, February 6, 2020 7:39 PM
  • jrv, thank you for your input. Any input is helpful, although the MessageBox is still coming up behind other Windows. :-(


    "Fate rarely calls upon us at a moment of our choosing…"

    Thursday, February 6, 2020 8:12 PM
  • The script I posted will not hide the message box.  If you have changed it then it is likely something else you are doing that is causing this.


    \_(ツ)_/

    Thursday, February 6, 2020 10:47 PM
  • Good morning jrv,

    I ran the script you posted, without any modifications and the message box still comes up behind other Windows.

    And since you mentioned learning PowerShell, I have not found any real documentation on using pop-up Windows.  I have just been grabbing bits and pieces from Google searches.

    I'm signed up with a Pluralsight account, but even then not finding much on pop-up dialog Windows.

    I thank you for your help. :-)


    "Fate rarely calls upon us at a moment of our choosing…"

    Friday, February 7, 2020 1:39 PM
  • If you need more control over the message box, try using the Visual Basic method:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic") 
    $Result = [Microsoft.VisualBasic.Interaction]::MsgBox("You clicked Cancel. Would you like to Retry or Cancel?", "RetryCancel,SystemModal,Question,DefaultButton1", "Select a location")


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by os_car Friday, February 7, 2020 6:56 PM
    Friday, February 7, 2020 4:50 PM
  • Thank you, Rich

    That worked.


    "Fate rarely calls upon us at a moment of our choosing…"

    • Marked as answer by os_car Friday, February 7, 2020 6:17 PM
    • Unmarked as answer by os_car Friday, February 7, 2020 6:56 PM
    Friday, February 7, 2020 6:17 PM