PowerShell函数返回混乱

时间:2019-10-23 14:34:32

标签: winforms function powershell return-value

我有一个函数正在尝试根据选定的单选按钮返回字符串。我唯一的问题是我无法理解为什么还要在返回字符串中添加一个Action。 我认为这是导致问题的代码的一部分:

# Add an OK button
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '400,200'
$OKButton.Size = '100,40' 
$OKButton.Text = 'OK'
$OKButton.Add_Click({ 
    $Form.Close()
})

$Form.Controls.Add($OKButton)
$Form.Controls.Add($GroupBox)

$Form.ShowDialog()

$ClickedRadioButton = $GroupBox.Controls | Where-Object -FilterScript {$_.Checked}
$Store_List = @("CDJ","OCC","OAG","FLM","HYU","KYS","MAZ","TOY")
$Department_List = "S","V","P","E","A","H","C","I","O"

$selectedButton = $labels.IndexOf($ClickedRadioButton.Text)
$selectedText = ""
switch($formType){
    1{$selectedText = $Store_List[$selectedButton]; break}
    2{$selectedText = $Department_List[$selectedButton]; break}
}

return $selectedText

这是我在调用上面的函数的主要函数中的代码

# Declaring Computer Name
$ComputerName = ""
$ComputerName = Generate_Form(1)
$temp = Generate_Form(2)
$ComputerName = $ComputerName  + $temp
Write-Output "Computer Name: " $ComputerName

我注意到删除$Form.ShowDialog()可以得到我想要的结果。 这是我得到的输出的结果:

Computer Name: 
Cancel
CDJ
Cancel
S

这是我删除$Form.ShowDialog()时的输出:

Computer Name: 
CDJS

我想在这里回答的两个主要问题是

  1. PowerShell为什么要在返回的字符串中添加“取消”?
  2. 如何在没有换行的情况下将返回的字符串添加到先前的字符串?

示例:

"Computer Name: CDJS"

代替

"Computer Name: 

CDJ

S"

0 个答案:

没有答案