簡體   English   中英

如何將CheckedListBox中的Checked Items添加到Combobox(下拉列表)並在未選中時從Combobox中刪除它們?

[英]How do I add Checked Items from a CheckedListBox to a Combobox (dropdown) and remove them from the Combobox when unchecked?

我應該首先說我是PowerShell的新手,我還處於學習階段。 我遇到了障礙,任何幫助都會受到贊賞。

我有以下代碼:

# LOAD WINFORMS ASSEMBLY
[reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
[reflection.assembly]::LoadWithPartialName( "System.Drawing")

# CREATE FORMS
$Form = New-Object Windows.Forms.Form
$Form.text = "Post-Image Configuration Tool"

$Form.Width = 900
$Form.Height = 560
$Form.BackColor = "#3a73b8"
$Form.ForeColor = "White"
$Form.FormBorderStyle = "None"
$Form.StartPosition = "CenterScreen"

# START NETWORK CONFIGURATION PAGE
$GetConnectedAdapters = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionStatus = 2" | Select-Object NetConnectionID, Name, MACAddress

$netConfigList1 = New-Object System.Windows.Forms.CheckedListBox
$netConfigList1.Location = New-Object System.Drawing.Size(310,300) 
$netConfigList1.Size = New-Object System.Drawing.Size(480,180) 
$netConfigList1.Height = 100
$netConfigList1.BackColor = "#3a73b8"
$netConfigList1.ForeColor = "White"
$netConfigList1.BorderStyle = "None"
$netConfigList1.Font = $ListFont
$netConfigList1.add_SelectedIndexChanged({ListNetAdapters})

$netConfigListAdapters = @()
ForEach ($i in $GetConnectedAdapters.NetConnectionID){
    $GetAdapterName = Get-WmiObject -Class Win32_NetworkAdapter |Where {$_.NetConnectionID -eq $i} | Select-Object Name, NetConnectionID, MACAddress
    $AdapterName = $i +" - " + "("+ $GetAdapterName.Name +")"
    $netConfigListAdapters += ,$AdapterName
}
$netConfigList1.Items.AddRange($netConfigListAdapters)

$netConfigSubtext5 = New-Object Windows.Forms.Label
$netConfigSubtext5.Location = New-Object Drawing.Point 290,400
$netConfigSubtext5.Size = New-Object Drawing.Point 590,20
$netConfigSubtext5.text = "• Select the Standby Adapter:"
$netConfigSubtext5.font = $SubTextFont

$netConfigComboBox1 = New-Object System.Windows.Forms.ComboBox 
$netConfigComboBox1.Location = New-Object System.Drawing.Size(310,420) 
$netConfigComboBox1.Size = New-Object System.Drawing.Size(260,20)
$netConfigComboBox1.Font = $SubTextFont
$netConfigComboBox1.DropDownStyle = "DropDownList"

[void] $netConfigComboBox1.Items.Add("None (All Adapters Active)")

$NetConfiguration = $netConfigList1,$netConfigSubtext5,$netConfigComboBox1

# CREATE FUNCTIONS

Function ListNetAdapters
{
    $RemoveItems = @()
    $AddItems = @()

    for($index =0; $index -lt $netConfigList1.Items.Count; $index++)
    {
        $test = $netConfigList1.Items | Where-Object { $netConfigList1.Items.IndexOf($index) }

        if($netConfigList1.GetItemChecked($index) -AND $netConfigComboBox1.Items -notcontains $test)
        {
            $AddItems += ,$test 
        }
        ForEach($i in $netConfigComboBox1.Items){
            IF(($netConfigList1.CheckedItems -notcontains $i) -AND ($i -ne 'None (All Adapters Active)')){$RemoveItems += ,$i}
        }

    }
    ForEach ($i in $RemoveItems){$netConfigComboBox1.Items.Remove($i)}
    ForEach ($i in $AddItems){$netConfigComboBox1.Items.Add($i)}
}  


Function AddNetConfiguration
{
    ForEach ($i in $NetConfiguration){$form.controls.add($i)}
}


AddNetConfiguration


# DISPLAY FORM
$form.ShowDialog()

基本上,我想要完成的是您在Windows Server 2012/2012 R2中的NIC團隊的高級設置中看到的內容。 我希望在CheckedListBox中選擇的網絡適配器填充在ComboBox中,如果未選中則將其刪除。

我已經在我的Windows 7 PC上安裝了WMF 4.0,這似乎運行良好,但我在Windows Server 2012中得到了“System.Object []”。所以我顯然錯過了大局或做錯了什么。

Windows Server 2012附帶PowerShell v3.0,您必須使用WMF4.0

答案從編輯轉移到問題

修復$ListNetAdapters函數后,我能夠使它工作。 我想以前我過於復雜了。

Function ListNetAdapters
{
    $RemoveItems = @()
    $AddItems = @()

    ForEach($checkedItem in $netConfigList1.CheckedItems){
        IF($netConfigComboBox1.Items -notcontains $checkedItem){$AddItems += ,$checkedItem}
    }

    ForEach($item2Badded in $AddItems){$netConfigComboBox1.Items.Add($item2Badded)}

    ForEach($dropdownItem in $netConfigComboBox1.Items){
        IF($netConfigList1.CheckedItems -notcontains $dropdownItem){$RemoveItems += ,$dropdownItem}
    }

    ForEach($item2Bremoved in $RemoveItems){
        IF($item2Bremoved -ne 'None (All Adapters Active)'){$netConfigComboBox1.Items.Remove("$item2Bremoved")}
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM