Make WPF windows topmost in Powershell ISE

Welcome Forums General PowerShell Q&A Make WPF windows topmost in Powershell ISE

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 8 months ago.

  • Author
    Posts
  • #64137

    Participant
    Points: 0
    Rank: Member

    Here I have the same MessageBox function realized in Winforms and WPF. They show up as expected if I simply call them, but if I call them after a Get-Credential prompt as in this example they are hidden behind the ISE window. I'm running PS 5.0 on Windows 10. On Windows 7 and PS ISE 5.0 the Winforms version shows up topmost but WPF does not. I tried adding $Form.Focus() or $Window.Focus() with no effect. How can I ensure that the user gets error messages?

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName PresentationFramework
    
    function MessageBoxWF ($Message, $Title) {
    	    $Form = New-Object 'System.Windows.Forms.Form'
    	    $Form.TopMost = $true
    	    [System.Windows.Forms.MessageBox]::Show($Form, $Message, $Title, 0, 48)
    	    $Form.Close()
    	    $Form.Dispose()
    }
    
    function MessageBoxWPF ($Message, $Title) {
            $Window = New-Object 'System.Windows.Window'
    	    $Window.TopMost = $true
    	    [System.Windows.MessageBox]::Show($Window, $Message, $Title, 'OK', 'Exclamation')
    }
    
    $cred = Get-Credential 'User'
    
    MessageBoxWPF "Error message." "Error" > $null
  • #64162

    Participant
    Points: 21
    Rank: Member

    This has been a known issue with the ise for as long as I can remember. The workaround is to never use ise;-)

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName PresentationFramework

    try{

    $cred = get-credential

    }catch{

    $msgbox= New-Object -ComObject WScript.Shell
    $msgbox.popup('error')

    }

  • #64195

    Participant
    Points: 0
    Rank: Member

    Thanks Dan, that Windows Script Host popup method works like a charm is ISE both on Win 10 and Win 7. It even makes sound if you decorate it properly. Great!

The topic ‘Make WPF windows topmost in Powershell ISE’ is closed to new replies.