Topmost not working ONLY when compiled exe

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
CermakPOI
Posts: 38
Last visit: Sun Dec 27, 2020 8:53 am

Topmost not working ONLY when compiled exe

Post by CermakPOI »

Product, version and build: PowerShell Studio 2017 v5.4.135
(*** Please do not write "latest" as a version, specify a version number ***)
32 or 64 bit version of product: 64
Operating system: Windows Server 2012 R2 / Windows 10
32 or 64 bit OS: 64
PowerShell Version: 5.1 / 5.0

I want to show a topmost messagebox. It's working perfectly when running from ps-promt or Exportet Forms project.
It does NOT show topmost when i run it from a compiled exe. (STA checked, Tried PS 3.0 and 5.0)

Of course all the values are filled.

Code: Select all

	$FrmMsgBox = New-Object 'System.Windows.Forms.Form'
				$FrmMsgBox.TopMost = $true
				$FrmMsgBox.Focus()
				$FrmMsgBox.BringToFront()
				#Write-StatusLog 0 "[Show-MessageBox]Topmost: FrmMsgBox `r`n$(($FrmMsgBox | fl | out-string).trim())" -calling -Autocolor
				$MsgResult = [System.Windows.Forms.MessageBox]::Show(
				$FrmMsgBox,
				"$Text",
				"$Title",
				"$MsgBoxButtons",
				"$Icon",
				[System.Windows.Forms.MessageBoxDefaultButton]::Button1
				)
				$FrmMsgBox.Close()
				$FrmMsgBox.Dispose() 

User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am

Re: Topmost not working ONLY when compiled exe

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]

The Focus method will will not work until the Form is display. Also try using the Select instead of Focus.
David
SAPIEN Technologies, Inc.

User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am

Re: Topmost not working ONLY when compiled exe

Post by davidc »

You need not create a Form object to display a message box.
David
SAPIEN Technologies, Inc.

User avatar
CermakPOI
Posts: 38
Last visit: Sun Dec 27, 2020 8:53 am

Re: Topmost not working ONLY when compiled exe

Post by CermakPOI »

I need to create a form object to show the messagebox as topmost.
This is working ALWAYS - except when calling hte function from a powershell studio compiled Exe (That's why i posted it originally to this Forum)

Paste This code in a powershell Window and see if you get a topmost messagebox:

Code: Select all

$Text = "MyTopmost Messagebox"
$Title ="MyTitle"
$Icon = "Info"
$MsgBoxButtons = "OK"
$FrmMsgBox = New-Object 'System.Windows.Forms.Form'
$FrmMsgBox.TopMost = $true
$FrmMsgBox.Focus()
$FrmMsgBox.BringToFront()
#Write-StatusLog 0 "[Show-MessageBox]Topmost: FrmMsgBox `r`n$(($FrmMsgBox | fl | out-string).trim())" -calling -Autocolor
$MsgResult = [System.Windows.Forms.MessageBox]::Show(
$FrmMsgBox,
"$Text",
"$Title",
"$MsgBoxButtons",
"$Icon",
[System.Windows.Forms.MessageBoxDefaultButton]::Button1
)
$FrmMsgBox.Close()
$FrmMsgBox.Dispose()
Then try this code inside a compiled "Windows forms" package.

jvierra
Posts: 14704
Last visit: Sat Feb 27, 2021 5:46 am
Answers: 8
Has voted: 2 times
Been upvoted: 6 times

Re: Topmost not working ONLY when compiled exe

Post by jvierra »

You cannot create a form then call a messagebox and have the non-displayed form center the messagebox. In PSS it will use PSS as the form to center on. In an plain EXE it will position to the console window if it is visible.

This is all you need or can use:
  1. $result = [System.Windows.Forms.MessageBox]::Show('MyTopmost Messagebox','MyTitle','Ok','Info','Button1')
You should try to avoid the habit of placing double quotes around everything and string variables do not need to be quoted.

User avatar
CermakPOI
Posts: 38
Last visit: Sun Dec 27, 2020 8:53 am

Re: Topmost not working ONLY when compiled exe

Post by CermakPOI »

But it's working in PSS - just not topmost.

jvierra
Posts: 14704
Last visit: Sat Feb 27, 2021 5:46 am
Answers: 8
Has voted: 2 times
Been upvoted: 6 times

Re: Topmost not working ONLY when compiled exe

Post by jvierra »

Working in PSS? I thought it wasn't working in the EXE?

PSS is not the host. It cannot be topmost to PSS no matter what you do.

Locked