Powershell窗体意外关闭



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

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

可能b/c您正在将Dialog Result赋值为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

最新更新