Winforms 如何将选中项从CheckedListBox添加到Combobox(下拉列表)中,并在未选中时将其从Combobox中删除?
首先,我应该说我是PowerShell的新手,我还处于学习阶段。我遇到了路障,任何帮助都将不胜感激 我有以下代码:Winforms 如何将选中项从CheckedListBox添加到Combobox(下拉列表)中,并在未选中时将其从Combobox中删除?,winforms,powershell,powershell-4.0,Winforms,Powershell,Powershell 4.0,首先,我应该说我是PowerShell的新手,我还处于学习阶段。我遇到了路障,任何帮助都将不胜感激 我有以下代码: # LOAD WINFORMS ASSEMBLY [reflection.assembly]::LoadWithPartialName( "System.Windows.Forms") [reflection.assembly]::LoadWithPartialName( "System.Drawing") # CREATE FORMS $Form = New-Object Wi
# 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中,如果未选中,则将其删除
我已经在我的Windows7电脑上安装了WMF4.0,这似乎运行得很好,但我在WindowsServer2012中得到了“System.Object[]”。因此,我显然忽略了大局或做错了什么。Windows Server 2012随PowerShell v3.0提供,您必须将其升级到WMF4.0Windows 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")}
}
}
编辑从问题中提出的答案
在我修复了$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")}
}
}
谢谢你的回复。但是,我的Windows 7 PC上已经安装了WMF 4.0,Windows Server 2012附带了Powershell 4.0。尽管如此,我还是通过更正我的ListNetAdapters函数解决了这个问题。:)谢谢你的回复。但是,我的Windows 7 PC上已经安装了WMF 4.0,Windows Server 2012附带了Powershell 4.0。尽管如此,我还是通过更正我的ListNetAdapters函数解决了这个问题。:)