Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Powershell how do I bring a messagebox to the foreground, and change focus to a button in the message box

Tags:

powershell

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.

like image 570
freakostudent Avatar asked Sep 19 '25 15:09

freakostudent


1 Answers

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"

like image 104
Bratch Avatar answered Sep 23 '25 05:09

Bratch