2

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.

ppiotrek
  • 55
  • 2
  • 2
  • 8

3 Answers3

1

1st example:

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')

2nd example:

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
}
Ste
  • 1,729
  • 1
  • 17
  • 27
1

Try like this

$topmost = New-Object 'System.Windows.Forms.Form' -Property @{TopMost=$true}
$form.ShowDialog($topmost)

This will activate AlwaysOnTop, and this window will remain on top of other normal windows even if those windows are activated.


If you just want to make the form OnTop (not Always) you can use this

$form.Add_Load({
  $form.Activate()
})

The windows will appear on top over other normal windows on load. But when activating other normal windows, this window will be send to back.

crisc2000
  • 1,082
  • 13
  • 19
-5

If you have your primary topmost form that you want to override the topmost setting in your first form (Call that your secondary form) then add the following to your "Primary" form

[void][reflection.assembly]::loadwithpartialname("System.Windows.Forms") 
# Form settings
$formPrimary = New-Object System.Windows.Forms.Form
$formPrimary.Text = "Primary Form"
$formPrimary.StartPosition = 4
$formPrimary.ClientSize = "200,200"
$formPrimary.Topmost = $True
$formSecondary.Topmost = $False
$formPrimary.ShowDialog()

This should set your secondary form back to it's normal state and allow your primary form to assume the topmost position. This is only in theory as I sam not familiar with getting more then one windows form open at tjhe same time since powershell only creates modal dialog boxes as far as I can tell so I am unable to test this solution yet.