Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airreggui.ps

(view source code of airreggui.ps as plain text)

  1. <#
  2. .SYNOPSIS
  3. Search for aircraft photos on Airport-data.com by the aircraft's registration number
  4.  
  5. .DESCRIPTION
  6. This simple GUI script allows you to enter an aircraft registration and then search Airport-data.com for photos and specifications of that aircraft.
  7. Interactive usage: enter an aircraft registration in the input texbox and press Enter or click the Search button to search Airport-data.com's website for photos and detailed specifications.
  8. Command line usage: run this script with -Help parameter to view its command line syntax.
  9.  
  10. .NOTES
  11. Dashes are mandatory in many aircraft registrations, but not all, e.g. PH-PBY and N1944S will work, but PHPBY and N-1944S won't.
  12.  
  13. Invalid command line switches starting with a dash will trigger a PowerShell error message.
  14.  
  15. All command line switches starting with a slash will trigger this script's help screen.
  16.  
  17. This script can be extended by using downloaded local aircraft registration databases; see this script's web page (see links section) for details.
  18.  
  19. Return code ("ErrorLevel") is 1 in case of errors, otherwise 0.
  20.  
  21. .PARAMETER AircraftRegistration
  22. (optional) A valid aircraft registration, e.g. PH-PBY or N1944S or OK-NUL.
  23. Aircraft registration may be entered in the GUI if not specified on the command line.
  24.  
  25. .PARAMETER SearchEngine
  26. (optional) Alternative search engine if no match was found on Airport-data.com.
  27. Valid SearchEngine values are 'DuckDuckGo', 'Google' (default) or 'StartPage'.
  28. Useful only if local database extension scripts are installed.
  29. Command line only, cannot be changed interactively in the GUI.
  30.  
  31. .PARAMETER Hide
  32. Hides the console window (GUI remains visible).
  33. Command line only, cannot be changed interactively in the GUI.
  34.  
  35. .PARAMETER Debug
  36. Shows some progress messages, especially usefull when using local databases.
  37. Command line only, cannot be changed interactively in the GUI.
  38.  
  39. .PARAMETER Version
  40. Show this script's version number.
  41. If combined with -Verbose show full script path, version number and last/modified/release date.
  42.  
  43. .PARAMETER Help
  44. Shows this script's help screen, with more details than the Help button's help text.
  45.  
  46. .EXAMPLE
  47. powershell . ./AirRegGUI.ps1
  48. Will open the GUI with empty input textbox, the console remains visible
  49.  
  50. .EXAMPLE
  51. powershell . ./AirRegGUI.ps1 -Hide
  52. Will open the GUI with empty input textbox and hide the console
  53.  
  54. .EXAMPLE
  55. powershell . ./AirRegGUI.ps1 N1944S
  56. Will open the GUI with N1944S in input textbox, and start search immediately, the console remains visible
  57.  
  58. .EXAMPLE
  59. powershell . ./AirRegGUI.ps1 -AircraftRegistration N1944S
  60. Will open the GUI with N1944S in input textbox, and start search immediately, the console remains visible
  61.  
  62. .EXAMPLE
  63. powershell . ./AirRegGUI.ps1 PH-PBY -Hide
  64. Will open the GUI with PH-PBY in input textbox, start search immediately, and hide the console
  65.  
  66. .EXAMPLE
  67. powershell . ./AirRegGUI.ps1 -AircraftRegistration PH-PBY -Hide
  68. Will open the GUI with PH-PBY in input textbox, start search immediately, and hide the console
  69.  
  70. .LINK
  71. Script written by Rob van der Woude:
  72. https://www.robvanderwoude.com/airreg.php
  73.  
  74. .LINK
  75. Airport-data API courtesy of Airport-data.com:
  76. http://www.airport-data.com/api/doc.php
  77.  
  78. .LINK
  79. Script created in Visual Studio Community 2019 with a trial version of Ironman Software's PowerShell Pro Tools Suite:
  80. https://ironmansoftware.com/powershell-pro-tools/
  81.  
  82. .LINK
  83. Capturing Enter key (as a work-around for the form's AcceptButton not working as expected) by sodawillow on StackOverflow.com:
  84. https://stackoverflow.com/a/41441704
  85.  
  86. .LINK
  87. Code to hide console window by Anthony on StackOverflow.com:
  88. http://stackoverflow.com/a/15079092
  89.  
  90. .LINK
  91. Use of Shown event (to make sure the GUI is visible before starting an optional immediate search) by Matthias Schippling on StackOverflow.com:
  92. https://stackoverflow.com/a/219155
  93.  
  94. .LINK
  95. Capture -Debug and -Verbose parameter by mklement0 on StackOverflow.com:
  96. https://stackoverflow.com/a/48643616
  97.  
  98. .LINK
  99. Embedded icon based on code by Theo on StackOverflow.com:
  100. https://stackoverflow.com/a/53377253
  101.  
  102. .LINK
  103. Airplane icon image courtesy of Icons8.com:
  104. https://icons8.com/
  105. #>
  106.  
  107. param (
  108. 	[parameter( ValueFromPipeline )]
  109. 	[ValidatePattern("(^\s*$|[\?/]|^\w[\w-]{2,3}\w+)")]
  110. 	[string]$AircraftRegistration = '',
  111. 	[parameter( ValueFromPipeline )]
  112. 	[ValidateSet('DuckDuckGo','Google','StartPage')]
  113. 	[string]$SearchEngine = 'Google',
  114. 	[switch]$Hide,
  115. 	[switch]$Help,
  116. 	[switch]$Version
  117. )
  118.  
  119. $progver = "2.15"
  120.  
  121. $AircraftRegistration = $AircraftRegistration.ToUpper( )
  122. $Manufacturer = ''
  123. $Model = ''
  124. [bool]$Debug = $Debug -or ( $PSBoundParameters.ContainsKey( 'Debug' ) )
  125. [bool]$Verbose = ( $PSBoundParameters.ContainsKey( 'Verbose' ) )
  126. $global:notfound = $true
  127.  
  128. if ( $Version ) {
  129. 	if ( $Verbose ) {
  130. 		$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  131. 		if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  132. 			"`"{0}`", Version {1}, release date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  133. 		} else {
  134. 			# if last modified time is not equal to program version, the script has been tampered with
  135. 			"`"{0}`", Version {1}, last modified date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  136. 		}
  137. 	} else {
  138. 		$progver
  139. 	}
  140. 	exit 0
  141. }
  142.  
  143. if ( $Help -or ( $AircraftRegistration -match "[/\?]" ) ) {
  144. 	Clear-Host
  145. 	Write-Host ( "`"{0}`", Version {1}" -f ( "$PSCommandPath" -replace "$PSScriptRoot","" ), $progver ) -NoNewline
  146. 	$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  147. 	if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  148. 		Write-Host ", release date " -NoNewline
  149. 	} else {
  150. 		# if last modified time is not equal to program version, the script has been tampered with
  151. 		Write-Host ", last modified date " -NoNewline
  152. 	}
  153. 	Write-Host $lastmod.ToString( "yyyy-MM-dd" )
  154. 	Write-Host
  155. 	Get-Help $PSCommandPath -Full
  156. 	exit -1
  157. }
  158.  
  159. $global:airportdatalink = ''
  160. $Debug = ( $PSBoundParameters.ContainsKey( 'Debug' ) )
  161. $defaulthelptext = "Enter an aircraft registration number, e.g. `"PH-PBY`" and click the Search button.`n`n" + `
  162.                    "This script will then search Airport-Data.com for photos of the aircraft with that registration.`n`n" + `
  163.                    "If found, a thumbnail of the photo will be displayed in this script's window, plus a button to view the photo full size in your default browser.`n`n" + `
  164.                    "{0} 2022 Rob van der Woude`nhttps://www.robvanderwoude.com" -f [char]0x00A9
  165.  
  166. # Search query templates
  167. if ( $SearchEngine -match '^DuckDuckGo$' ) {
  168. 	$SearchEngine = 'DuckDuckGo'
  169. 	$searchtemplate = "https://duckduckgo.com/html/?q=x{0}"
  170. } elseif ( $SearchEngine -match '^Google$' ) {
  171. 	$SearchEngine = 'Google'
  172. 	$searchtemplate = "https://www.google.com/search?q={0}"
  173. } elseif ( $SearchEngine -match '^Startpage$' ) {
  174. 	$SearchEngine = 'StartPage'
  175. 	$searchtemplate = "https://startpage.com/do/search?cat=web&cmd=process_search&language=english&engine0=v1all&query={0}"
  176. } else {
  177. 	$SearchEngine = 'Google'
  178. 	$searchtemplate = "https://www.google.com/search?q={0}" # Google by default
  179. 	if ( -not $Quiet ) {
  180. 		Write-Host ( "Invalid -SearchEngine value `"{0}`", using Google instead" -f $SearchEngine )
  181. 	}
  182. }
  183. if ( $Debug ) {
  184. 	Write-Host ( "Search Engine: {0}" -f $SearchEngine )
  185. 	Write-Host ( "Search template: {0}" -f $searchtemplate )
  186. }
  187.  
  188. #######################################
  189. # Hide console window                 #
  190. # by Anthony on StackOverflow.com     #
  191. # http://stackoverflow.com/a/15079092 #
  192. #######################################
  193.  
  194. $signature1 = @'
  195. public static void ShowConsoleWindow( int state )
  196. {
  197. 	var handle = GetConsoleWindow( );
  198. 	ShowWindow( handle, state );
  199. }
  200.  
  201. [System.Runtime.InteropServices.DllImport( "kernel32.dll" )]
  202. static extern IntPtr GetConsoleWindow( );
  203.  
  204. [System.Runtime.InteropServices.DllImport( "user32.dll" )]
  205. static extern bool ShowWindow( IntPtr hWnd, int nCmdShow );
  206. '@
  207.  
  208. # Hide console if requested
  209. if ( $Hide ) {
  210. 	$hideconsole = Add-Type -MemberDefinition $signature1 -Name Hide -Namespace HideConsole -ReferencedAssemblies System.Runtime.InteropServices -PassThru
  211. 	$hideconsole::ShowConsoleWindow( 0 )
  212. }
  213.  
  214. #######################
  215. # End of Hide console #
  216. #######################
  217.  
  218. ###########################
  219. # Start of Event handlers #
  220. ###########################
  221.  
  222. $ButtonHelp_Click = {
  223. 	$scriptname = $PSCommandPath.Substring( $PSScriptRoot.Length )
  224. 	$inserttext = "If local database extensions are installed, a local search will be initiated if no match was found online on Airport-data.com.`n" + `
  225. 	              "Local searches take considerably longer than online searches!`n" + `
  226. 	              "See https://www.robvanderwoude.com/airreg.php for details on local database extensions.`n`n" + `
  227. 	              "For detailed help use the command:`n`n" + `
  228. 	              "      . '.{0}' -Help`n`nor:`n`n" -f $scriptname + `
  229. 				  "      . '.{0}' /?`n`nor:`n`n" -f $scriptname + `
  230. 	              "      Get-Help '.{0}' -Full`n`n" -f $scriptname + `
  231. 				  "Do you want to open this script's web page now?`n`n{1}" -f $scriptname, [char]0x00A9
  232. 	$helptext = $defaulthelptext -replace [char]0x00A9, $inserttext
  233. 	$title = "Help for {0}" -f $AirRegGUI.Text
  234. 	$answer = [System.Windows.Forms.MessageBox]::Show( $helptext, $title, 'YesNo' )
  235. 	if ( $answer -eq 'Yes' ) {
  236. 		Start-Process 'https://www.robvanderwoude.com/airreg.php'
  237. 	}
  238. }
  239.  
  240. $ButtonOpenLink_Click = {
  241. 	Start-Process $global:airportdatalink -WindowStyle 1
  242. }
  243.  
  244. $ButtonSearch_Click = {
  245. 	Search-Aircraft
  246. }
  247.  
  248. $TextboxReg_KeyUp = {
  249. 	Validate-Input( $_.KeyCode )
  250. }
  251.  
  252. $TextboxReg_TextChanged = {
  253. 	Validate-Input
  254. }
  255.  
  256. $Window_Load = {
  257. 	$AirRegGUI.Text = "AirRegGUI.ps1 - Version {0}" -f $progver # window title
  258. 	$LabelHelptext.Text = $defaulthelptext # help text in window
  259. }
  260.  
  261. $Window_Shown = {
  262. 	if ( ![string]::IsNullOrWhiteSpace( $AircraftRegistration ) ) {
  263. 		$TextboxReg.Text = $AircraftRegistration # enter specified aircraft registration in input textbox
  264. 		$TextboxReg.SelectionStart = $TextboxReg.Text.Length # move cursor to end of input text
  265. 		$TextboxReg.SelectionLength = 0
  266. 		# Start search immediately
  267. 		[System.Windows.Forms.Application]::DoEvents( ) # start updating GUI
  268. 		Start-Sleep -Seconds 1 # allow GUI update to complete
  269. 		Validate-Input( 'Return' )
  270. 	}
  271. }
  272.  
  273. #########################
  274. # End of Event handlers #
  275. #########################
  276.  
  277. function Check-Update( ) {
  278. 	$ProgressPreference = 'SilentlyContinue'
  279. 	$checkupdate = ( Invoke-WebRequest -Uri 'https://www.robvanderwoude.com/updates/airreg.txt' -TimeoutSec 10 )
  280. 	$ProgressPreference = 'Continue'
  281. 	if ( $checkupdate.StatusCode -eq 200 ) {
  282. 		$latestversion = [float]$checkupdate.Content
  283. 		$currentversion = [float]$progver
  284. 		if ( $latestversion -gt $currentversion ) {
  285. 			$title = 'Update Available'
  286. 			$message = ( "An update for this script is available.`nYour current version is {0}, the latest version available is {1}.`n`n" + `
  287. 			             "Do you want to open the download page for the update?" -f ( $currentversion -replace ",","." ), ( $latestversion -replace ",","." ) )
  288. 			$buttons = [System.Windows.Forms.MessageBoxButtons]::YesNo
  289. 			if ( [System.Windows.Forms.MessageBox]::Show( $message, $title, $buttons ) -eq 'Yes' ) {
  290. 				Start-Process 'https://www.robvanderwoude.com/airreg.php'
  291. 			}
  292. 		}
  293. 	}
  294. }
  295.  
  296. function Search-LocalDB( $message = 'Not found' ) {
  297. 	[OutputType([bool])]
  298. 	$prefix = ''
  299. 	$registration = $TextboxReg.Text
  300. 	$found = $false
  301. 	if ( $registration.Contains( '-' ) ) {
  302. 		$prefix = $registration.Split( '-' )[0]
  303. 	} else {
  304. 		if ( Test-Path ( "$PSScriptRoot\AirReg{0}Cmd.ps1" -f $registration.Substring( 0, 2 ) ) -PathType 'Leaf' ) {
  305. 			$prefix = $registration.Substring( 0, 2 )
  306. 		} elseif ( Test-Path ( "$PSScriptRoot\AirReg{0}Cmd.ps1" -f $registration.Substring( 0, 1 ) ) -PathType 'Leaf' ) {
  307. 			$prefix = $registration.Substring( 0, 1 )
  308. 		} else {
  309. 			$prefix = ''
  310. 		}
  311. 	}
  312. 	if ( -not [string]::IsNullOrWhiteSpace( $prefix ) ) {
  313. 		$localDBscript = ( Join-Path -Path $PSScriptRoot -ChildPath ( "AirReg{0}Cmd.ps1" -f $prefix ) )
  314. 		if ( $Debug ) {
  315. 			Write-Host ( "Found local '{0}' database file `"{1}`"" -f $prefix, $localDBscript )
  316. 		}
  317. 		$ButtonOpenLink.Visible = $false
  318. 		$LabelPhotographer.Visible = $false
  319. 		$PictureboxThumbnail.Visible = $false
  320. 		$ButtonOpenLink.Enabled = $false
  321. 		$LabelHelptext.Text = "Searching local database, this may take a while..."
  322. 		$LabelHelptext.Visible = $true
  323. 		[System.Windows.Forms.Application]::DoEvents( ) # update GUI
  324.  
  325. 		$Manufacturer = ''
  326. 		$Model = ''
  327.  
  328. 		if ( $Debug ) {
  329. 			$Stopwatch = [System.Diagnostics.Stopwatch]::StartNew( )
  330. 			Write-Host ( "Start searching local database at {0}" -f ( Get-Date ) )
  331. 			if ( ( Get-Content $localDBscript ) -match 'TerminateExcel' ) {
  332. 				Write-Host ( "Starting extension script `"{0}`" with Arguments {1} -TerminateExcel -Debug at {2}" -f $localDBscript, $registration, $stopwatch.Elapsed )
  333. 				. "$localDBscript" -Registration $registration -TerminateExcel -Debug
  334. 			} else {
  335. 				Write-Host ( "Starting extension script `"{0}`" with Arguments {1} -Debug at {2}" -f $localDBscript, $registration, $stopwatch.Elapsed )
  336. 				. "$localDBscript" -Registration $registration -Debug
  337. 			}
  338. 			Write-Host ( "Finished at {0} (time elapsed {1})" -f ( Get-Date ), $Stopwatch.Elapsed )
  339. 			$Stopwatch.Stop( )
  340. 			Write-Host ( "Registration = {0}" -f $registration )
  341. 			Write-Host ( "Manufacturer = {0}" -f $Manufacturer )
  342. 			Write-Host ( "Model        = {0}" -f $Model )
  343. 		} else {
  344. 			if ( ( Get-Content "$localDBscript" ) -match 'TerminateExcel' ) {
  345. 				. "$localDBscript" -Registration $registration -TerminateExcel
  346. 			} else {
  347. 				. "$localDBscript" -Registration $registration
  348. 			}
  349. 		}
  350. 		if ( $Model ) {
  351. 			if ( $Manufacturer ) {
  352. 				$LabelHelptext.Text = ( "{0} {1}" -f $Manufacturer, $Model )
  353. 				$searchquery = ( ( ( "{0} {1} {2}" -f $Manufacturer, $Model, $registration ) -replace " ","+" ) -replace ",","" )
  354. 			} else {
  355. 				$LabelHelptext.Text = $Model
  356. 				$searchquery = ( ( ( "{0} {1}" -f $Model, $registration ) -replace " ","+" ) -replace ",","" )
  357. 			}
  358. 			$global:airportdatalink = ( $searchtemplate -f $searchquery )
  359. 			$ButtonOpenLink.Enabled = $true
  360. 			$ButtonOpenLink.Text = ( 'Search on {0}' -f $SearchEngine )
  361. 			$ButtonOpenLink.Visible = $true
  362. 			[System.Windows.Forms.Application]::DoEvents( ) # start updating GUI
  363. 			Start-Sleep -Seconds 1 # allow GUI update to complete
  364. 			$found = $true
  365. 			$global:notfound = $false
  366. 		} else {
  367. 			if ( Test-Path -Path "$PSScriptRoot\$prefix" -PathType 'Container' ) {
  368. 				$local = 'local'
  369. 			} else {
  370. 				$local = 'online'
  371. 			}
  372. 			$message = ( "$message, and not found in {0} {1} database either" -f $local, $prefix )
  373. 			$LabelHelptext.Text = $message
  374. 			[System.Windows.Forms.Application]::DoEvents( ) # start updating GUI
  375. 			Start-Sleep -Seconds 1 # allow GUI update to complete
  376. 			Search-OpenSky $message
  377. 		}
  378. 	} else {
  379. 		$message = ( "$message, no local {0} database found" -f $prefix )
  380. 		$LabelHelptext.Text = $message
  381. 		[System.Windows.Forms.Application]::DoEvents( ) # start updating GUI
  382. 		Start-Sleep -Seconds 1 # allow GUI update to complete
  383. 		Search-OpenSky $message
  384. 	}
  385. 	$found
  386. }
  387.  
  388. function Search-OpenSky( $message = 'Not found' ) {
  389. 	[OutputType([bool])]
  390. 	$registration = $TextboxReg.Text
  391. 	$found = $false
  392. 	$openskyscript = ( Join-Path $PSScriptRoot 'AirRegOpenSkyCmd.ps1' )
  393. 	if ( Test-Path -Path $openskyscript -PathType 'Leaf' ) {
  394. 		$LabelHelptext.Text = ( "{0}, searching OpenSky database, this may take a minute..." -f $LabelHelptext.Text )
  395. 		[System.Windows.Forms.Application]::DoEvents( ) # start updating GUI
  396. 		Start-Sleep -Seconds 1 # allow GUI update to complete
  397. 		if ( $Debug ) {
  398. 			Write-Host ( "Found OpenSky extension `"{0}`"" -f $openskyscript )
  399. 			. "$openskyscript" -Registration $registration -NoBreak -Debug
  400. 		} else {
  401. 			. "$openskyscript" -Registration $registration -NoBreak
  402. 		}
  403. 		if ( $Model ) {
  404. 			if ( $Manufacturer ) {
  405. 				$LabelHelptext.Text = ( ( "{0} {1}" -f $Manufacturer, $Model ) -replace "`"","" )
  406. 				$searchquery = ( ( ( ( "{0} {1} {2}" -f $Manufacturer, $Model, $registration ) -replace " ","+" ) -replace ",","" ) -replace "`"","" )
  407. 			} else {
  408. 				$LabelHelptext.Text = $Model
  409. 				$searchquery = ( ( ( ( "{0} {1}" -f $Model, $registration ) -replace " ","+" ) -replace ",","" ) -replace "`"","" )
  410. 			}
  411. 			$global:airportdatalink = ( $searchtemplate -f $searchquery )
  412. 			$ButtonOpenLink.Enabled = $true
  413. 			$ButtonOpenLink.Text = ( 'Search on {0}' -f $SearchEngine )
  414. 			$ButtonOpenLink.Visible = $true
  415. 			$found = $true
  416. 			$global:notfound = $false
  417. 		} else {
  418. 			$LabelHelptext.Text = "$message, nor in OpenSky database"
  419. 		}
  420. 	} else {
  421. 		$LabelHelptext.Text = $message
  422. 	}
  423. 	$global:notfound
  424. }
  425.  
  426. function Search-Aircraft( ) {
  427. 	$url = ( 'http://www.airport-data.com/api/ac_thumb.json?r={0}' -f $TextboxReg.Text )
  428. 	$ProgressPreference = 'SilentlyContinue'
  429. 	$json = ( Invoke-WebRequest -Uri $url -TimeoutSec 10 ).Content | ConvertFrom-Json -ErrorAction Stop
  430. 	$ProgressPreference = 'Continue'
  431. 	if ( $json.status -eq 200 ) {
  432. 		$LabelHelptext.Visible = $false
  433. 		$PictureboxThumbnail.ImageLocation = $json.data.image
  434. 		$LabelPhotographer.Visible = $true
  435. 		$LabelPhotographer.Text = ( "Photographer: {0}" -f $json.data.photographer )
  436. 		$ButtonOpenLink.Enabled = $true
  437. 		$global:airportdatalink = $json.data.link
  438. 		$PictureboxThumbnail.Visible = $true
  439. 		$ButtonOpenLink.Visible = $true
  440. 		$aircraftdescription = ( ( Invoke-WebRequest -Uri $global:airportdatalink ).Content | Select-String -Pattern ( '{0},[^\",]+' -f $TextboxReg.Text ) )
  441. 		if ( $aircraftdescription.Matches.Count -gt 0 ) {
  442. 			$aircrafttype = $aircraftdescription.Matches[0].Value
  443. 			$aircrafttype = $aircrafttype -replace ( "^{0}, " -f $TextboxReg.Text ), ''
  444. 			$aircrafttype = $aircrafttype -replace "^[12][0-9][0-9][0-9] ", ''
  445. 			$aircrafttype = $aircrafttype -replace " C/N.+$", ''
  446. 			$LabelAircraftType.Visible = $true
  447. 			$LabelAircraftType.Text = $aircrafttype
  448. 		}
  449. 	} else {
  450. 		$ButtonOpenLink.Visible = $false
  451. 		$LabelPhotographer.Visible = $false
  452. 		$PictureboxThumbnail.Visible = $false
  453. 		$ButtonOpenLink.Enabled = $false
  454. 		if ( Search-LocalDB( ( 'Status {0}, error {1} (aircraft registration not found on Airport-Data.com)' -f $json.status, $json.error ) ) ) {
  455. 			if ( $global:notfound ) {
  456. 				[console]::Beep( 500, 500 ) # beep if registration not found
  457. 			}
  458. 		}
  459. 		$LabelHelptext.Visible = $true
  460. 	}
  461. }
  462.  
  463. function Validate-Input( $key = $null ) {
  464. 	if ( $key -eq 'Return' ) {
  465. 		Search-Aircraft
  466. 	} else {
  467. 		$cursorpos = $TextboxReg.SelectionStart + $TextboxReg.SelectionLength
  468. 		$inputtext = $TextboxReg.Text.ToUpper( )
  469. 		$inputtext = $inputtext -replace "[^A-Z\d\-]",""
  470. 		if ( $inputtext -cne $TextboxReg.Text ) {
  471. 			if ( $inputtext -cne $TextboxReg.Text.ToUpper( ) ) {
  472. 				[console]::Beep( 500, 500 ) # beep in case of invalid character
  473. 			}
  474. 			$TextboxReg.Text = $inputtext
  475. 			$TextboxReg.SelectionStart = [Math]::Min( $cursorpos, $inputtext.Length )
  476. 			$TextboxReg.SelectionLength = 0
  477. 		}
  478. 		if ( ( $inputtext -replace "-","" ).Length -ge 4 ) {
  479. 			$ButtonSearch.Enabled = $true
  480. 		} else {
  481. 			$ButtonSearch.Enabled = $false
  482. 		}
  483. 		$LabelHelptext.Visible = $true
  484. 		$LabelHelptext.Text = $defaulthelptext
  485. 		$LabelAircraftType.Text = ''
  486. 		$LabelAircraftType.Visible = $false
  487. 		$LabelPhotographer.Text = ''
  488. 		$LabelPhotographer.Visible = $false
  489. 		$PictureboxThumbnail.Visible = $false
  490. 		$ButtonOpenLink.Text = 'View photo on Airport-data.com'
  491. 		$ButtonOpenLink.Enabled = $false
  492. 		$ButtonOpenLink.Visible = $false
  493. 	}
  494. }
  495.  
  496. ############################################
  497. # GUI form design was saved separately by  #
  498. # Visual Studio/Ironman Software's         #
  499. # PowerShell Pro Tools Suite as            #
  500. # AirRegGUI.designer.ps1 and then manually #
  501. # embedded in AirRegGUI.ps1 by yours truly #
  502. #                                          #
  503. # Start of embedded AirRegGUI.designer.ps1 #
  504. ############################################
  505.  
  506. [void][System.Reflection.Assembly]::Load( 'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' )
  507. [void][System.Reflection.Assembly]::Load( 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' )
  508.  
  509. $AirRegGUI = New-Object -TypeName System.Windows.Forms.Form
  510. [System.Windows.Forms.TextBox]$TextboxReg = ( New-Object -TypeName System.Windows.Forms.TextBox )
  511. [System.Windows.Forms.Button]$ButtonSearch = ( New-Object -TypeName System.Windows.Forms.Button )
  512. [System.Windows.Forms.Button]$ButtonHelp = ( New-Object -TypeName System.Windows.Forms.Button )
  513. [System.Windows.Forms.Button]$ButtonOpenLink = ( New-Object -TypeName System.Windows.Forms.Button )
  514. [System.Windows.Forms.PictureBox]$PictureboxThumbnail = ( New-Object -TypeName System.Windows.Forms.PictureBox )
  515. [System.Windows.Forms.Label]$LabelHelptext = ( New-Object -TypeName System.Windows.Forms.Label )
  516. [System.Windows.Forms.Label]$LabelAircraftType = ( New-Object -TypeName System.Windows.Forms.Label )
  517. [System.Windows.Forms.Label]$LabelPhotographer = ( New-Object -TypeName System.Windows.Forms.Label )
  518. ( [System.ComponentModel.ISupportInitialize]$PictureboxThumbnail ).BeginInit( )
  519. $AirRegGUI.SuspendLayout( )
  520.  
  521. $TextboxReg.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]37, [System.Int32]41 ) )
  522. $TextboxReg.MaxLength = [System.Int32]8
  523. $TextboxReg.Name = [System.String]'TextboxReg'
  524. $TextboxReg.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]100, [System.Int32]26 ) )
  525. $TextboxReg.TabIndex = [System.Int32]0
  526. $TextboxReg.Add_TextChanged( $TextboxReg_TextChanged )
  527. $TextboxReg.Add_KeyUp( $TextboxReg_KeyUp )
  528.  
  529. $ButtonSearch.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]159, [System.Int32]40 ) )
  530. $ButtonSearch.Name = [System.String]'ButtonSearch'
  531. $ButtonSearch.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]75, [System.Int32]28 ) )
  532. $ButtonSearch.TabIndex = [System.Int32]1
  533. $ButtonSearch.Text = [System.String]'Search'
  534. $ButtonSearch.UseVisualStyleBackColor = $true
  535. $ButtonSearch.Add_Click( $ButtonSearch_Click )
  536. $ButtonSearch.Enabled = $false
  537.  
  538. $ButtonHelp.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]256, [System.Int32]40 ) )
  539. $ButtonHelp.Name = [System.String]'ButtonHelp'
  540. $ButtonHelp.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]75, [System.Int32]28 ) )
  541. $ButtonHelp.TabIndex = [System.Int32]2
  542. $ButtonHelp.Text = [System.String]'Help'
  543. $ButtonHelp.UseVisualStyleBackColor = $true
  544. $ButtonHelp.Add_Click( $ButtonHelp_Click )
  545.  
  546. $ButtonOpenLink.Enabled = $false
  547. #$ButtonOpenLink.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]37, [System.Int32]315 ) )
  548. $ButtonOpenLink.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]37, [System.Int32]355 ) )
  549. $ButtonOpenLink.Name = [System.String]'ButtonOpenLink'
  550. $ButtonOpenLink.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]294, [System.Int32]28 ) )
  551. $ButtonOpenLink.TabIndex = [System.Int32]4
  552. $ButtonOpenLink.Text = [System.String]'View photo on Airport-data.com'
  553. $ButtonOpenLink.UseVisualStyleBackColor = $true
  554. $ButtonOpenLink.Add_Click( $ButtonOpenLink_Click )
  555. $ButtonOpenLink.Visible = $false
  556.  
  557. $PictureboxThumbnail.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]82, [System.Int32]101 ) )
  558. $PictureboxThumbnail.Name = [System.String]'PictureboxThumbnail'
  559. $PictureboxThumbnail.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]200, [System.Int32]150 ) )
  560. $PictureboxThumbnail.TabIndex = [System.Int32]3
  561. $PictureboxThumbnail.TabStop = $false
  562. $PictureboxThumbnail.Visible = $false
  563.  
  564. $LabelHelptext.AutoSize = $true
  565. $LabelHelptext.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]37, [System.Int32]101 ) )
  566. $LabelHelptext.MaximumSize = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]294, [System.Int32]0 ) )
  567. $LabelHelptext.Name = [System.String]'LabelHelptext'
  568. $LabelHelptext.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]275, [System.Int32]60 ) )
  569. $LabelHelptext.TabIndex = [System.Int32]6
  570. $LabelHelptext.Text = [System.String]''
  571.  
  572. $LabelAircraftType.AutoSize = $true
  573. #$LabelAircraftType.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]33, [System.Int32]273 ) )
  574. $LabelAircraftType.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]33, [System.Int32]273 ) )
  575. $LabelAircraftType.Name = [System.String]'$LabelAircraftType'
  576. $LabelAircraftType.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]110, [System.Int32]20 ) )
  577. $LabelAircraftType.TabIndex = [System.Int32]5
  578. $LabelAircraftType.Text = [System.String]'Photographer:'
  579. $LabelAircraftType.Visible = $false
  580.  
  581. $LabelPhotographer.AutoSize = $true
  582. #$LabelPhotographer.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]33, [System.Int32]273 ) )
  583. $LabelPhotographer.Location = ( New-Object -TypeName System.Drawing.Point -ArgumentList @( [System.Int32]33, [System.Int32]315 ) )
  584. $LabelPhotographer.Name = [System.String]'LabelPhotographer'
  585. $LabelPhotographer.Size = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]110, [System.Int32]20 ) )
  586. $LabelPhotographer.TabIndex = [System.Int32]5
  587. $LabelPhotographer.Text = [System.String]'Photographer:'
  588. $LabelPhotographer.Visible = $false
  589.  
  590. # Icon embedding code by Theo on https://stackoverflow.com/a/53377253
  591. # Airplane icon image courtesy of https://icons8.com/
  592. $iconBase64 = 'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABY0lEQVR42mNkGGDAiEduIhDbAfFfIE4F4vP0doA5EB+HqnkO5T+mpwNAYD0QB0DZV4HYCog/0dMBmkB8BYiZoPw9QOwFxL/p5QAQmA/ECUj8BUCcSE8HyAHxbSBmQxKrB+ImejkABCYAcT4S/z80VBZRwwEgg/cyQOIaFxAF4ntAzIMk9guIPYF4H6UOeMgAyetGQPwBj9oGBkjQI4OPDJCccY0SB2wHYg8GSJYLwqOWFxoKImjiIA9YAPELch3QC8RFUH4BA6QExAUKgbgPi/g5ILYF4m/kOACUpeZB+aB4tQHi0zjUcwDxTQZIzkAHW4HYD4j/keoAUBF7AknsAQMkr+MCzlDfYgOgaLxIhL2gNHcKiHeBHACKW6oWrySAOlg5AEpIcpSYRCb4MigcMOBRMOCJcMCz4YAXRANeFA94ZTTg1TExgKYNEkJgwJtkA9ooHfBm+YB2TAa8azbgnVO6AAAjEXEBu6DybwAAAABJRU5ErkJggg=='
  593. $iconBytes  = [Convert]::FromBase64String( $iconBase64 )
  594. $stream     = New-Object IO.MemoryStream( $iconBytes, 0, $iconBytes.Length )
  595. $stream.Write( $iconBytes, 0, $iconBytes.Length );
  596. $iconImage  = [System.Drawing.Image]::FromStream( $stream, $true )
  597.  
  598. $AirRegGUI.ClientSize = ( New-Object -TypeName System.Drawing.Size -ArgumentList @( [System.Int32]384, [System.Int32]441 ) )
  599. $AirRegGUI.Controls.Add( $LabelHelptext )
  600. $AirRegGUI.Controls.Add( $LabelAircraftType )
  601. $AirRegGUI.Controls.Add( $LabelPhotographer )
  602. $AirRegGUI.Controls.Add( $ButtonOpenLink )
  603. $AirRegGUI.Controls.Add( $PictureboxThumbnail )
  604. $AirRegGUI.Controls.Add( $ButtonHelp )
  605. $AirRegGUI.Controls.Add( $ButtonSearch )
  606. $AirRegGUI.Controls.Add( $TextboxReg )
  607. $AirRegGUI.Font = ( New-Object -TypeName System.Drawing.Font -ArgumentList @( [System.String]'Microsoft Sans Serif', [System.Single]12, [System.Drawing.FontStyle]::Regular, [System.Drawing.GraphicsUnit]::Point, ( [System.Byte][System.Byte]0) ) )
  608. $AirRegGUI.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
  609. $AirRegGUI.MaximizeBox = $false
  610. $AirRegGUI.Name = [System.String]'AirRegGUI'
  611. $AirRegGUI.SizeGripStyle = [System.Windows.Forms.SizeGripStyle]::Hide
  612. $AirRegGUI.StartPosition = 'CenterScreen'
  613. $AirRegGUI.Icon = [System.Drawing.Icon]::FromHandle( ( New-Object System.Drawing.Bitmap -Argument $stream ).GetHIcon( ) ) # Icon embedding code by Theo on https://stackoverflow.com/a/53377253
  614. $AirRegGUI.Text = [System.String]'AirRegGUI.ps1'
  615. $AirRegGUI.Add_Load( $Window_Load )
  616. $AirRegGUI.Add_Shown( $Window_Shown )
  617. $AirRegGUI.Add_HelpRequested( $ButtonHelp_Click )
  618. ( [System.ComponentModel.ISupportInitialize]$PictureboxThumbnail ).EndInit( )
  619. $AirRegGUI.ResumeLayout( $false )
  620. $AirRegGUI.PerformLayout( )
  621.  
  622. Add-Member -InputObject $AirRegGUI -Name base -Value $base -MemberType NoteProperty
  623. Add-Member -InputObject $AirRegGUI -Name TextboxReg -Value $TextboxReg -MemberType NoteProperty
  624. Add-Member -InputObject $AirRegGUI -Name ButtonSearch -Value $ButtonSearch -MemberType NoteProperty
  625. Add-Member -InputObject $AirRegGUI -Name ButtonHelp -Value $ButtonHelp -MemberType NoteProperty
  626. Add-Member -InputObject $AirRegGUI -Name PictureboxThumbnail -Value $PictureboxThumbnail -MemberType NoteProperty
  627. Add-Member -InputObject $AirRegGUI -Name ButtonOpenLink -Value $ButtonOpenLink -MemberType NoteProperty
  628. Add-Member -InputObject $AirRegGUI -Name LabelHelptext -Value $LabelHelptext -MemberType NoteProperty
  629. Add-Member -InputObject $AirRegGUI -Name LabelAircraftType -Value $LabelAircraftType -MemberType NoteProperty
  630. Add-Member -InputObject $AirRegGUI -Name LabelPhotographer -Value $LabelPhotographer -MemberType NoteProperty
  631.  
  632. ##########################################
  633. # End of embedded AirRegGUI.designer.ps1 #
  634. ##########################################
  635.  
  636. # Check for program updates and notify user if available
  637. Check-Update
  638.  
  639. # Open GUI window
  640. [void] $AirRegGUI.ShowDialog( )
  641.  
  642. # Clean up (ShowDialog does not always clean up by itself)
  643. [void] $AirRegGUI.Dispose( )
  644.  

page last modified: 2022-10-20