Powershell-如何将变量从列表框项目更改为组合框项目?

时间:2019-07-11 17:48:48

标签: winforms powershell variables combobox listbox

我正在尝试通过删除用户可以选择的项目列表框来清理我的GUI,这些项目列表框可以在Powershell中(使用DropDownList锁定用户输入)添加到受约束的组合框中。到目前为止,我还无法获得该变量来反映所选的组合框项目。

理想情况下,我想让$ Env等于在组合框中选择的替换$ listbox的文本字符串

我尝试遵循powershell combobox items to variable无济于事,因为我不了解如何使用“ SelectedIndexChanged”事件...我可能只是不完全了解如何使用此语法... a代码示例会很棒。

我拥有的当前代码:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form 
$form.Text = "Select an environment"
$form.Size = New-Object System.Drawing.Size(190,250) 
$form.StartPosition = "CenterScreen"
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(10,180)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Please select an environment:"
$form.Controls.Add($label) 
$listBox = New-Object System.Windows.Forms.ListBox 
$listBox.Location = New-Object System.Drawing.Point(10,40) 
$listBox.Size = New-Object System.Drawing.Size(150,20) 
$listBox.Height = 140

[void] $listBox.Items.AddRange(@("PROD", "QA1", "QA2", "TR"))
$form.Controls.Add($listBox) 
$form.Topmost = $True
do
{
    $result = $form.ShowDialog()
    if ($ListBox.SelectedIndices.Count -lt 1 -and $result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        Write-Warning 'Nothing was selected, please select a server.'
    }
}
until (($result -eq [System.Windows.Forms.DialogResult]::OK -and $listBox.SelectedIndices.Count -ge 1) -or $result -ne [System.Windows.Forms.DialogResult]::OK)
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        $Env -eq $listBox.SelectedItem
    }

1 个答案:

答案 0 :(得分:0)

以下是在Powershell中使用事件的快速指南:
在WinForms对象上使用| Get-Member -MemberType Event,以获取可用事件的列表> $OKButton | Get-Member -MemberType Event
一旦事件触发,添加应执行的脚本块。 > $OKButton.add_Click({$ScriptGoesHere})。注意脚本块(PS> help about_scopes)的范围
我用事件而不是循环来重新编写脚本,并在重要部分添加了注释。如果您有很多事情要做,事件就容易处理得多。但是范围是一个缺点。

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form 
$form.Text = "Select an environment"
$form.Size = New-Object System.Drawing.Size(380,250) 
$form.StartPosition = "CenterScreen"
$form.Topmost = $True

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(10,180)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
#$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK    #Moved to the event
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Please select an environment:"
$form.Controls.Add($label) 

$listBox = New-Object System.Windows.Forms.ListBox 
$listBox.Location = New-Object System.Drawing.Point(10,40) 
$listBox.Size = New-Object System.Drawing.Size(150,140) # the second number is the height
#$listBox.Height = 140
[void] $listBox.Items.AddRange(@("PROD", "QA1", "QA2", "TR"))
$form.Controls.Add($listBox) 


$comboBox = New-Object System.Windows.Forms.ComboBox 
$comboBox.Location = New-Object System.Drawing.Point(190,40) 
$comboBox.Size = New-Object System.Drawing.Size(150,20) 
[void] $comboBox.Items.AddRange(@("PROD", "QA1", "QA2", "TR"))
$form.Controls.Add($comboBox)

#region Events
#register some events that trigger actions


$listBox.add_SelectedIndexChanged({    
    #The event is actually $listBox.SelectedIndexChanged but we want to ADD an action. add_SelectedIndexChanged is not listed in get-member.
    $ListSelected = $listBox.SelectedItem
    Write-Host "ListSelected = $ListSelected"
    # You will note that $ListSelected is not available outside the event yet. It is in the scope of the scriptblock.
})


$comboBox.add_SelectedIndexChanged({
    #To prevent trouble with the scope, define the variables in a higher scope
    $script:SelectedItem = $comboBox.SelectedItem
    $global:SelectedIndex = $comboBox.SelectedItem
    write-host "SelectedItem = $SelectedItem"
})

$script:OKScript = {
    # or define the whole ScriptBlock in a higher scope...
    if( $comboBox.SelectedIndex -ge 0){

        $Env = $comboBox.SelectedItem    #Be carefull $env is the beginning of environmental variables like $env:path
        $form.DialogResult = [System.Windows.Forms.DialogResult]::OK
        $form.close()

    }else{
        Write-Warning 'Nothing was selected, please select a server.'
    }
}

# ...and DotSource it to execute it in the same scope:
$OKButton.add_Click({. $OKScript})


#endregion

$result = $form.ShowDialog()


<# This would work but we use events
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $SelectedItem = $comboBox.SelectedItem
    $SelectedIndex = $comboBox.SelectedItem
}
#>


write-host "`r`nform is closed`r`nhere are the results:"

"ListSelected = $ListSelected"
"result = $result"

"env = $env"
"SelectedIndex = $SelectedIndex"
"SelectedItem = $SelectedItem"