Advertisement
CPUTek1

GuessGame_PSWinForms.ps1

Sep 20th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #   LOAD THE WINFORMS ASSEMBLY
  2. [reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  3. function do_exit
  4. {
  5.     $form.Close()
  6. }
  7. #   CREATE THE FORM
  8. $form=New-Object Windows.Forms.Form
  9. $form.Width="350"
  10. $form.Text="Powershell Windows Form Guessing Game"
  11. #   CREATE CONTROLS
  12. ##  LABEL
  13. $label=New-Object Windows.Forms.Label
  14. $label.text="I'm thinking of a number between 1 and 100 can you guess it"
  15. $label.Location = New-Object drawing.Point 10,10
  16. $label.size= new-object Drawing.Point 300,15
  17. ##  LABEL2
  18. $label2=New-Object Windows.Forms.Label
  19. $label2.text=""
  20. $label2.Location = New-Object drawing.Point 10,100
  21. $label2.size= new-object Drawing.Point 300,15
  22. ##  TEXTBOX
  23. $textfield=New-Object Windows.Forms.TextBox
  24. $textfield.size=new-object Drawing.Point 300,15
  25. $textfield.Location = New-Object drawing.Point 10,30
  26. ##  BUTTON
  27. $button=New-Object windows.Forms.Button
  28. $button.Location = New-Object drawing.Point 10,50
  29. $button.text="Guess"
  30. #   EXIT BUTTON
  31. $exitbutton=New-Object windows.Forms.Button
  32. $exitbutton.Location = New-Object drawing.Point 10,200
  33. $exitbutton.text="Exit"
  34.  
  35. #   GENERATE A RANDOM
  36. [int]$Number=Get-Random -Minimum 1 -Maximum 100
  37. ##  COUNTER
  38. $global:Counter=0
  39. #   EVENT HANDLER FOR BUTTON CLICK
  40. $button.add_click({
  41.     $Guess=$textfield.Text
  42.     $int = [int]$Guess
  43.     $global:Counter++
  44.     if($int -eq $Number)
  45.     {
  46.         $label.Text="You Win!"
  47.         $label2.Text="It Took You $global:Counter Tries"
  48.     }
  49.     else
  50.     {
  51.         if ($int -lt $Number)
  52.         {
  53.             $label.text = "Try Number $global:Counter"
  54.             $label2.text="Guess is too small"
  55.         }
  56.         else
  57.         {
  58.             $label.text = "Try Number $global:Counter"
  59.             $label2.text="Guess is too big"
  60.         }
  61.     }
  62. })
  63. #   EXIT BUTTON CLICK EVENT
  64. $exitbutton.add_click({do_exit})
  65.    
  66. #   ADD THE CONTROLS TO THE FORM
  67. $form.Controls.Add($label)
  68. $form.Controls.Add($label2)
  69. $form.Controls.Add($button)
  70. $form.Controls.Add($exitbutton)
  71. $form.Controls.Add($textfield)
  72.  
  73. #   DISPLAY THE FORM
  74. $form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement