4

In my script when I have a messagebox open, the messagebox always opens in the background, behind all other applications and windows running. I am trying to do two things (I apologize if they should be two questions, but I think are closely related) 1. I want the message box to be show in the front of all applications when it needs presents, and 2. I want to change the focus to the 'okay' button on the messagebox.

First I load the messagebox $hide = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

Then it opens when needed.

do
{
    $attempt = LogIn    #recieves true (see line 233 comments)
    $bad++  #attempt = true


    if ($bad -lt $allock -and $attempt -eq $false)
    { 
        Messagebox -Message "Wrong Password" -BoxTitle "Error"
    }

    if ($bad -eq $allock -and $attempt -eq $false) #false, skip
    {     
        Messagebox -Message "Five Tries, You are almost Locked out, Try again" -BoxTitle "Error"  
    }

    if ($bad -eq $lockout-and $attempt -eq $false) #false, skip
    {
       Messagebox -Message "Six tries, you are probably locked out. Good Luck." -BoxTitle "Lockout?"
       break
    }
    #attempt = true     
} while ($attempt -ne $true) #true, exit

While there is a plethora of information on how to use messageboxes in powershell, I was unable to find any information on bringing the box to the front, and changing the focus to the enter button on the box. I would appreciate any guidance or pointers to information. Thank you.

freakostudent
  • 105
  • 1
  • 2
  • 9
  • 2
    Can you show us the `Messagebox` function? You need to supply a `MessageBoxDefaultButton` to the `Show()` method in order for it to focus on a specific button – Mathias R. Jessen Dec 15 '15 at 21:50

2 Answers2

2

From http://blogs.msdn.com/b/sonam_rastogi_blogs/archive/2015/09/01/keeping-powershell-visualbasic-messagebox-in-focus.aspx

[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic") 
[Microsoft.VisualBasic.Interaction]::MsgBox("Processing Completed.", "YesNoCancel,SystemModal,Information,DefaultButton2", "Success")
[Microsoft.VisualBasic.Interaction]::MsgBox("Five Tries, You are almost Locked out, Try again", "OKOnly,SystemModal,Exclamation", "Lockout?")

The last example will also work with "OKOnly,SystemModal,Exclamation,DefaultButton1" but it's not required.

"There are various other combinations that are part of Message Box Style Enumerator

ApplicationModal, DefaultButton1, OkOnly, OkCancel, AbortRetryIgnore, YesNoCancel, YesNo, RetryCancel, Critical, Question, Exclamation, Information, DefaultButton2, DefaultButton3, SystemModal, MsgBoxHelp, MsgBoxSetForeground, MsgBoxRight, MsgBoxRtlReading"

Bratch
  • 4,103
  • 5
  • 27
  • 32
1

Setting the MessageBoxOptions to DefaultDesktopOnly will have the messagebox shown top most.

Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show(
  'Wrong Password','Error','Ok','Error','Ok','DefaultDesktopOnly'
)

References

Dennis
  • 871
  • 9
  • 29