PoshCode Logo PowerShell Code Repository

Start-TimeoutDialog by Grant Carthew 10 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2610"></script>download | new post

Displays a custom dialog box for a timeout period. The dialog box contains a message, two buttons and a countdown timer. The button text and other options are set through the parameters. The WSHShell.Popup has a bug so replace it with this script.

  1. <#
  2.     .Synopsis
  3.      Displays a message box that will timeout after a set number of seconds.
  4.      This script was written because WSHShell.Popup has a bug.
  5.      
  6.      Platform: Windows 7 32bit, Windows 2008 R2 64bit
  7.      Author: Grant Carthew.
  8.      Date: Apr 2011
  9.      Updated:
  10.  
  11.     .Description
  12.      When calling this script it creates a message box with custom settings
  13.      and displays the message.   There is a countdown timer displayed on the
  14.      message box to inform the user when the message will timeout.
  15.      
  16.      If the user presses Button1 then the text on Button1 is returned.
  17.      If the user presses Button2 then the text on Button2 is returned.
  18.      If the message box times out then the string TIMEOUT is returned.
  19.      
  20.     .Parameter Title
  21.      The title of the message box.
  22.      Default = "Timeout"
  23.      
  24.     .Parameter Message
  25.      The message to display on the message box.
  26.      Default = "Timeout Message"
  27.      
  28.     .Parameter Button1Text
  29.      The text to display on the left hand button.
  30.      This text is returned if the user clicks the button.
  31.          Button1 is the default accept button so ENTER will press Button1
  32.      Default = "OK"
  33.      
  34.     .Parameter Button2Text
  35.      The text to display on the right hand button.
  36.      This text is returned if the user clicks the button.
  37.          Button2 is the default cancel button so ESC will press Button2
  38.      Default = "Cancel"
  39.      
  40.     .Parameter Seconds
  41.      The timeout value for the message box.
  42.      If the message box is displayed for this amount of time then
  43.      it will close and the string TIMEOUT will be returned.
  44.      Default = 30
  45.      
  46.     .Example
  47.      Start-TimeoutDialog -Title "Shutdown" -Message "This system will shutdown in 50 minutes." -Seconds 3000
  48. #>
  49.  
  50. param
  51. (
  52.     [parameter(Mandatory=$false)]
  53.     [String]
  54.     $Title = "Timeout",
  55.    
  56.     [parameter(Mandatory=$false)]
  57.     [String]
  58.     $Message = "Timeout Message",
  59.    
  60.     [parameter(Mandatory=$false)]
  61.     [String]
  62.     $Button1Text = "OK",
  63.    
  64.     [parameter(Mandatory=$false)]
  65.     [String]
  66.     $Button2Text = "Cancel",
  67.    
  68.     [parameter(Mandatory=$false)]
  69.     [Int]
  70.     $Seconds = 30
  71. )
  72.  
  73. BEGIN
  74. {
  75.     Write-Verbose -Message ("Begin: " + $MyInvocation.MyCommand.Path)
  76.     [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
  77.     $F = $null
  78.     $B1 = $null
  79.     $B2 = $null
  80.     $L = $null
  81.     $TB = $null
  82.     $Timer = $null
  83.     $Result = $null
  84.     $TimeLeft = New-TimeSpan -Seconds $Seconds
  85.     $ONESEC = New-TimeSpan -Seconds 1
  86. }
  87.  
  88. PROCESS
  89. {
  90.     # Form
  91.     $F = New-Object -TypeName System.Windows.Forms.Form
  92.     $F.Text = $Title
  93.     $F.Size = New-Object -TypeName System.Drawing.Size(300,150)
  94.     $F.StartPosition = "CenterScreen"
  95.     $F.Topmost = $true
  96.     $F.KeyPreview = $true
  97.     $F.ShowInTaskbar = $false
  98.     $F.ShowIcon = $false
  99.     $F.FormBorderStyle = "FixedDialog"
  100.     $F.MaximizeBox = $false
  101.     $F.MinimizeBox = $false
  102.  
  103.     # Button One
  104.     $B1 = New-Object -TypeName System.Windows.Forms.Button
  105.     $B1.Size = New-Object -TypeName System.Drawing.Size(75,23)
  106.     $B1.Location = New-Object -TypeName System.Drawing.Size(125,90)
  107.     $B1.Text = $Button1Text
  108.     $B1.Add_Click({$Result=$Button1Text;$F.Close()})
  109.     $F.Controls.Add($B1)
  110.     $F.AcceptButton = $B1
  111.  
  112.     # Button Two
  113.     $B2 = New-Object -TypeName System.Windows.Forms.Button
  114.     $B2.Size = New-Object -TypeName System.Drawing.Size(75,23)
  115.     $B2.Location = New-Object -TypeName System.Drawing.Size(210,90)
  116.     $B2.Text = $Button2Text
  117.     $B2.Add_Click({$Result=$Button2Text;$F.Close()})
  118.     $F.Controls.Add($B2)
  119.     $F.CancelButton = $B2
  120.  
  121.     # Label
  122.     $L = New-Object -TypeName System.Windows.Forms.Label
  123.     $L.Size = New-Object -TypeName System.Drawing.Size(280,65)
  124.     $L.Location = New-Object -TypeName System.Drawing.Size(10,15)
  125.     $L.Text = $Message
  126.     $F.Controls.Add($L)
  127.  
  128.     # Textbox
  129.     $TB = New-Object -TypeName System.Windows.Forms.TextBox
  130.     $TB.Size = New-Object -TypeName System.Drawing.Size(100,20)
  131.     $TB.Location = New-Object -TypeName System.Drawing.Size(10,91)
  132.     $TB.TextAlign = "Center"
  133.     $TB.ReadOnly = $true
  134.     $TB.Text = [String]$TimeLeft.Hours + " : " + $TimeLeft.Minutes + " : " + $TimeLeft.Seconds
  135.     $F.Controls.Add($TB)
  136.    
  137.     # Windows Timer
  138.     $Timer = New-Object -TypeName System.Windows.Forms.Timer
  139.     $Timer.Interval = 1000
  140.     $Timer.Add_Tick({$TimeLeft = $TimeLeft - $ONESEC;$TB.Text = [String]$TimeLeft.Hours + " : " + $TimeLeft.Minutes + " : " + $TimeLeft.Seconds;if ($TimeLeft.TotalSeconds -lt 1) { $Result = "TIMEOUT"; $F.Close() } })
  141.     $Timer.Start()
  142.  
  143.     # Show
  144.     $F.Add_Shown({$F.Activate()})
  145.     $F.ShowDialog() | Out-Null
  146.     $Result
  147. }
  148.  
  149. END
  150. {
  151.     Write-Verbose -Message ("End: " + $MyInvocation.MyCommand.Name)
  152. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me