我是Powershell的新手,我正在尝试整理一个Powershell计划,通过Google Maps API计算距离。我可以在Powershell ISE中告诉变量" miles" (最终结果)在"得到距离时正确设置"单击但文本框中没有显示距离,表单将关闭。这是为什么?
[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']
}
答案 0 :(得分:1)
可能是b / c您正在将对话结果分配给OK。我修改了一下并评论说出来。工作正常:
[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