Can you point me how should I set a window to be topmost in Powershell? I used this code:
$form.TopMost = $True
And this works almost perfect. The problem I have is that there are two topmost windows and for some reason my form sometimes gets hidden and should always be on top.
This one is taken from this web page:
https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/keeping-msgbox-on-top
When you open a MsgBox dialog from PowerShell, the dialog window may sometimes not be visible and instead appears behind the PowerShell or ISE window.
To make sure a MsgBox dialog box appears in front of your PowerShell window, try this:
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::MsgBox('My message', 'YesNo,MsgBoxSetForeground,Information', 'MyTitle')
Key is the option MsgBoxSetForeground
. If you'd like to know what other options you can choose from, replace the second argument with nonsense text, and the error message will list all the other option names.
One is SystemModal
. If you use that instead of MsgBoxSetForeground
, then the MsgBox will not only appear in front, it will stay there. No other window can then overlap the dialog box until the user clicked one of its buttons.
SystemModal
is the bit that will set this to the foremost of all windows.
So use:
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.Interaction]::MsgBox('My message', 'YesNo,SystemModal,Information', 'MyTitle')
This is a directory picker taken from:
https://powershellone.wordpress.com/2016/05/06/powershell-tricks-open-a-dialog-as-topmost-window/
Add-Type -AssemblyName System.Windows.Forms
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowser.Description = 'Select the folder containing the data'
$result = $FolderBrowser.ShowDialog((New-Object System.Windows.Forms.Form -Property @{TopMost = $true }))
if ($result -eq [Windows.Forms.DialogResult]::OK){
$FolderBrowser.SelectedPath
}
else {
exit
}