PoshCode Logo PowerShell Code Repository

egg_timer (modification of post by tojo2000 view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1192"></script>download | new post

A script I submitted for Event 10 of the Scripting games. Displays a simple Windows Form that counts down three minutes. It makes a good example for using Windows forms.

  1. function GenerateForm {
  2.   [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
  3.   [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
  4.  
  5.   $form_main = New-Object System.Windows.Forms.Form
  6.   $reset_button = New-Object System.Windows.Forms.Button
  7.   $label1 = New-Object System.Windows.Forms.Label
  8.   $start_button = New-Object System.Windows.Forms.Button
  9.   $progressBar1 = New-Object System.Windows.Forms.ProgressBar
  10.   $timer1 = New-Object System.Windows.Forms.Timer
  11.   $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
  12.  
  13.   $start_button_OnClick = {
  14.     $timer1.Enabled = $true
  15.     $timer1.Start()
  16.     $start_button.Text = 'Countdown Started.'
  17.   }
  18.  
  19.   $reset_button_OnClick = {
  20.     $timer1.Enabled = $false
  21.     $progressBar1.Value = 0
  22.     $start_button.Text = 'Start'
  23.         $label1.Text = '3:00'
  24.   }
  25.  
  26.   $timer1_OnTick = {
  27.     $progressBar1.PerformStep()
  28.  
  29.     $time = 180 - $progressBar1.Value
  30.     [char[]]$mins = "{0}" -f ($time / 60)
  31.     $secs = "{0:00}" -f ($time % 60)
  32.  
  33.     $label1.Text = "{0}:{1}" -f $mins[0], $secs
  34.  
  35.     if ($progressBar1.Value -eq $progressBar1.Maximum) {
  36.       $timer1.Enabled = $false
  37.       $start_button.Text = 'FINISHED!'
  38.     }
  39.   }
  40.  
  41.   $OnLoadForm_StateCorrection = {
  42.   #Correct the initial state of the form to prevent the .Net maximized form issue
  43.   $form_main.WindowState = $InitialFormWindowState
  44.   }
  45.  
  46.   $System_Drawing_Size = New-Object System.Drawing.Size
  47.   $System_Drawing_Size.Width = 628
  48.   $System_Drawing_Size.Height = 295
  49.   $form_main.MaximumSize = $System_Drawing_Size
  50.  
  51.   $form_main.Text = 'Super Duper Over-engineered Egg Timer'
  52.   $form_main.MaximizeBox = $False
  53.   $form_main.Name = 'form_main'
  54.   $form_main.ShowIcon = $False
  55.   $System_Drawing_Size = New-Object System.Drawing.Size
  56.   $System_Drawing_Size.Width = 628
  57.   $System_Drawing_Size.Height = 295
  58.   $form_main.MinimumSize = $System_Drawing_Size
  59.   $form_main.StartPosition = 1
  60.   $form_main.DataBindings.DefaultDataSourceUpdateMode = 0
  61.   $System_Drawing_Size = New-Object System.Drawing.Size
  62.   $System_Drawing_Size.Width = 612
  63.   $System_Drawing_Size.Height = 259
  64.   $form_main.ClientSize = $System_Drawing_Size
  65.  
  66.   $reset_button.TabIndex = 4
  67.   $reset_button.Name = 'button2'
  68.   $System_Drawing_Size = New-Object System.Drawing.Size
  69.   $System_Drawing_Size.Width = 209
  70.   $System_Drawing_Size.Height = 69
  71.   $reset_button.Size = $System_Drawing_Size
  72.   $reset_button.UseVisualStyleBackColor = $True
  73.  
  74.   $reset_button.Text = 'Reset'
  75.   $reset_button.Font = New-Object System.Drawing.Font("Verdana",12,0,3,0)
  76.  
  77.   $System_Drawing_Point = New-Object System.Drawing.Point
  78.   $System_Drawing_Point.X = 362
  79.   $System_Drawing_Point.Y = 13
  80.   $reset_button.Location = $System_Drawing_Point
  81.   $reset_button.DataBindings.DefaultDataSourceUpdateMode = 0
  82.   $reset_button.add_Click($reset_button_OnClick)
  83.  
  84.   $form_main.Controls.Add($reset_button)
  85.  
  86.   $label1.TabIndex = 3
  87.   $label1.TextAlign = 32
  88.   $System_Drawing_Size = New-Object System.Drawing.Size
  89.   $System_Drawing_Size.Width = 526
  90.   $System_Drawing_Size.Height = 54
  91.   $label1.Size = $System_Drawing_Size
  92.   $label1.Text = '3:00'
  93.   $label1.Font = New-Object System.Drawing.Font("Courier New",20.25,1,3,0)
  94.  
  95.   $System_Drawing_Point = New-Object System.Drawing.Point
  96.   $System_Drawing_Point.X = 45
  97.   $System_Drawing_Point.Y = 89
  98.   $label1.Location = $System_Drawing_Point
  99.   $label1.DataBindings.DefaultDataSourceUpdateMode = 0
  100.   $label1.Name = 'label1'
  101.  
  102.   $form_main.Controls.Add($label1)
  103.  
  104.   $start_button.TabIndex = 2
  105.   $start_button.Name = 'button1'
  106.   $System_Drawing_Size = New-Object System.Drawing.Size
  107.   $System_Drawing_Size.Width = 310
  108.   $System_Drawing_Size.Height = 70
  109.   $start_button.Size = $System_Drawing_Size
  110.   $start_button.UseVisualStyleBackColor = $True
  111.  
  112.   $start_button.Text = 'Start'
  113.   $start_button.Font = New-Object System.Drawing.Font("Verdana",12,0,3,0)
  114.  
  115.   $System_Drawing_Point = New-Object System.Drawing.Point
  116.   $System_Drawing_Point.X = 45
  117.   $System_Drawing_Point.Y = 12
  118.   $start_button.Location = $System_Drawing_Point
  119.   $start_button.DataBindings.DefaultDataSourceUpdateMode = 0
  120.   $start_button.add_Click($start_button_OnClick)
  121.  
  122.   $form_main.Controls.Add($start_button)
  123.  
  124.   $progressBar1.DataBindings.DefaultDataSourceUpdateMode = 0
  125.   $progressBar1.Maximum = 180
  126.   $System_Drawing_Size = New-Object System.Drawing.Size
  127.   $System_Drawing_Size.Width = 526
  128.   $System_Drawing_Size.Height = 87
  129.   $progressBar1.Size = $System_Drawing_Size
  130.   $progressBar1.Step = 1
  131.   $progressBar1.TabIndex = 0
  132.   $System_Drawing_Point = New-Object System.Drawing.Point
  133.   $System_Drawing_Point.X = 45
  134.   $System_Drawing_Point.Y = 146
  135.   $progressBar1.Location = $System_Drawing_Point
  136.   $progressBar1.Style = 1
  137.   $progressBar1.Name = 'progressBar1'
  138.  
  139.   $form_main.Controls.Add($progressBar1)
  140.  
  141.   $timer1.Interval = 1000
  142.   $timer1.add_tick($timer1_OnTick)
  143.  
  144.   $InitialFormWindowState = $form_main.WindowState
  145.   $form_main.add_Load($OnLoadForm_StateCorrection)
  146.   $form_main.ShowDialog()| Out-Null
  147.  
  148. }
  149.  
  150. #Call the Function
  151. GenerateForm

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