Make WPF windows topmost in Powershell ISE

Tagged: , ,

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Mark Mark 3 months, 1 week ago.

  • Author
    Posts
  • #64137
    Profile photo of Mark
    Mark
    Participant

    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
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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
    Profile photo of Mark
    Mark
    Participant

    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!

You must be logged in to reply to this topic.