Reputation: 3
I'm new to Powershell and am trying to put together a Powershell program to calculate distances via Google Maps API. I can tell within Powershell ISE that the variable "miles" (final result) sets correctly when "get distance" is clicked but no distance is displayed in the textbox and the form closes. Why is that?
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Travel Calculator"
$objForm.Size = New-Object System.Drawing.Size(375,200)
$objForm.StartPosition = "CenterScreen"
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Size(10,10)
$label1.Size = New-Object System.Drawing.Size(40,15)
$label1.Text = "From:"
$objform.Controls.Add($label1)
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Size(10,58)
$label2.Size = New-Object System.Drawing.Size(40,15)
$label2.Text = "To:"
$objform.Controls.Add($label2)
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(250,27)
$OKButton.Size = New-Object System.Drawing.Size(92,68)
$OKButton.Text = "Get Distance"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$objForm.Controls.Add($OKButton)
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(10,27)
$DropDown.Size = new-object System.Drawing.Size(200,30)
$objForm.Controls.Add($DropDown)
$DropDown2 = new-object System.Windows.Forms.ComboBox
$DropDown2.Location = new-object System.Drawing.Size(10,74)
$DropDown2.Size = new-object System.Drawing.Size(200,30)
$objForm.Controls.Add($DropDown2)
$locations = "Los Angeles", "Chicago", "New York"
ForEach ($Item in $locations) {
$DropDown.Items.Add($Item) | out-null
$DropDown2.Items.Add($Item) | out-null
}
$textBox1 = New-Object System.Windows.Forms.TextBox
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 10
$System_Drawing_Point.Y = 125
$textBox1.Location = $System_Drawing_Point
$textBox1.text = $miles
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 100
$textBox1.Size = $System_Drawing_Size
$textBox1.TabIndex = 3
$objForm.Controls.Add($textBox1)
$result = $objForm.ShowDialog()
$from = $DropDown.SelectedItem.ToString()
$to = $DropDown2.SelectedItem.ToString()
if ($result -eq [System.Windows.Forms.DialogResult]::OK){
$request = ( Invoke-WebRequest "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&units=imperial&sensor=false" ).Content -match 'text" : "(?<content>.*)mi'
$miles = $matches['content']
}
Upvotes: 0
Views: 184
Reputation: 6605
Probably b/c you are assigning Dialog Result to OK. I modified it a bit and commented that out. Works fine:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
Function OKButton_Click {
$from = $DropDown.SelectedItem.ToString()
$to = $DropDown2.SelectedItem.ToString()
$request = ( Invoke-WebRequest "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&units=imperial&sensor=false" ).Content -match 'text" : "(?<content>.*)mi'
$miles = $matches['content']
$textbox1.text =$miles
}
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Travel Calculator"
$objForm.Size = New-Object System.Drawing.Size(375,200)
$objForm.StartPosition = "CenterScreen"
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Size(10,10)
$label1.Size = New-Object System.Drawing.Size(40,15)
$label1.Text = "From:"
$objform.Controls.Add($label1)
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Size(10,58)
$label2.Size = New-Object System.Drawing.Size(40,15)
$label2.Text = "To:"
$objform.Controls.Add($label2)
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(250,27)
$OKButton.Size = New-Object System.Drawing.Size(92,68)
$OKButton.Text = "Get Distance"
#$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$objForm.Controls.Add($OKButton)
$OKButton.add_Click({OKButton_Click})
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(10,27)
$DropDown.Size = new-object System.Drawing.Size(200,30)
$objForm.Controls.Add($DropDown)
$DropDown2 = new-object System.Windows.Forms.ComboBox
$DropDown2.Location = new-object System.Drawing.Size(10,74)
$DropDown2.Size = new-object System.Drawing.Size(200,30)
$objForm.Controls.Add($DropDown2)
$locations = "Los Angeles", "Chicago", "New York"
ForEach ($Item in $locations) {
$DropDown.Items.Add($Item) | out-null
$DropDown2.Items.Add($Item) | out-null
}
$textBox1 = New-Object System.Windows.Forms.TextBox
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 10
$System_Drawing_Point.Y = 125
$textBox1.Location = $System_Drawing_Point
$textBox1.text = $miles
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 100
$textBox1.Size = $System_Drawing_Size
$textBox1.TabIndex = 3
$objForm.Controls.Add($textBox1)
$objForm.ShowDialog() |Out-Null
Upvotes: 1