2014-04-16 73 views
0

我是Powershell的新手,我正试图组合一个Powershell程序来计算通过Google Maps API的距离。我可以告诉Powershell ISE,当单击“获取距离”但没有距离显示在文本框中并且表单关闭时,变量“英里数”(最终结果)会正确设置。这是为什么?Powershell窗体意外关闭

[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'] 

} 

回答

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