winforms - Winform:无法更新组合框
问题描述
我正在使用 Powershell 处理 Windows 窗体,它将查询打印服务器以查找所有已安装的打印机(功能:FetchPrinters)。我添加了一个按钮来运行 FetchPrinters 功能。
Function:
$Global:PrintServer = ""
function FetchPrinters {
$printerTxtFileLocation = 'C:\Temp\PrinterList.csv'
$outputBox.text = "Fetching list of printers on $Global:PrintServer"
# $Global:PrintServer = $Global:DropDownBox1.SelectedItem.ToString()
Get-Printer -ComputerName "$Global:PrintServer" | Select-Object -ExpandProperty Name | Sort-Object -CaseSensitive | Out-File -Force -Encoding utf8 -FilePath "$printerTxtFileLocation"
$PrinterList = Get-Content -Path "$printerTxtFileLocation" -Encoding UTF8 -Force
foreach ($Printer in $PrinterList) {
$DropDownBox2.ResetText()
[void] $DropDownBox2.Items.Add($Printer)
}
}
在 Dropdownbox1 中,将有一个站点(位置)列表,选择后将确定要查询的打印服务器。
$DropDownBox1 = New-Object System.Windows.Forms.ComboBox
$DropDownBox1.Text = "Select Site Name"
$DropDownBox1.Size = New-Object System.Drawing.Size(190, 20)
$DropDownBox1.Location = New-Object System.Drawing.Point(20, 20)
$Sites = @(
'Site 1',
'Site 2',
'Site 3',
'Site 4',
'Site 5',
'Site 6'
'Site 7'
)
ForEach ($Site in $Sites) {
$DropDownBox1.Items.Add($Site) | Out-Null
}
$Global:DropDownBox1_SelectedIndexChanged = {
Switch ($DropDownBox1.Text) {
'Site 1' { $Global:PrintServer = "\\Printserver1" }
'Site 2' { $PrintServer = "\\Printserver2" }
'Site 3' { $PrintServer = "\\Printserver3" }
'Site 4' { $PrintServer = "\\Printserver3" }
'Site 5' { $PrintServer = "\\Printserver4" }
'Site 6' { $PrintServer = "\\Printserver5" }
'Site 7' { $PrintServer = "\\Printserver6" }
}
}
$DropDownBox1.add_SelectedIndexChanged($Global:DropDownBox1_SelectedIndexChanged)
$Form.Controls.Add($DropDownBox1)
在 Dropdownbox2 中将有可安装的打印机列表
$DropDownBox2 = New-Object System.Windows.Forms.ComboBox
$DropDownBox2.Text = "Select the Printer to install"
$DropDownBox2.Location = New-Object System.Drawing.Size(20, 60)
$DropDownBox2.Size = New-Object System.Drawing.Size(180, 20)
$DropDownBox2.DropDownHeight = 200
$DropDownBox2.add_SelectedIndexChanged({ })
$Form.Controls.Add($DropDownBox2)
照原样,一切正常,我可以在 Drowndownbox1 中选择一个站点,点击 Fetch Printers 按钮,Dropdownbox2 显示该站点可用的打印机。
我遇到的问题是当我尝试选择另一个站点(以相同的形式)并点击 Fetch Printers 按钮时,$Printers 变量保留了它看起来像的旧数据,因此 Dropdownbox2 包含上一个查询中的打印机,和电流。
理想情况下,每当我选择一个站点,点击获取打印机时,dropdownbox2 只包含该站点的信息。
我已经尝试使用 Clear-Variable cmdlet,但它似乎并没有解决问题,但也有可能我做错了什么,并且没有正确考虑这一点。
任何帮助是极大的赞赏。
解决方案
看起来像添加:
$dropdownbox2.Items.Clear()
...对我的功能做我需要的。我可以发誓我试过这个,但也许语法错误。
推荐阅读
- django - 在 Django 中向序列化程序添加另一个类
- javascript - 传单控制不提供准确的侧边栏
- java - Java 编译问题——不允许使用默认方法
- javascript - 标签中的href与Angular中的window.open
- psychopy - Presenting only a subset of conditions
- python - Slicing dataframe by comparing all values of one column to all values of column of another dataframe
- c - malloc a struct in kernel header file error :invalid application of 'sizeof' to incomplete type
- hadoop - Hadoop 3.1.1 等和 sbin 文件
- java - Java Elastic Search Api: Unable to run simple example: org.elasticsearch.transport.NodeDisconnectedException:
- java - Spring数据jpa方法查询findWith