【问题标题】:DriveLetter ComboBox not populating correctlyDriveLetter ComboBox 未正确填充
【发布时间】:2021-06-10 11:36:27
【问题描述】:

我正在创建一个显示当前未使用/未分配的驱动器号的 GUI 脚本。 虽然该功能正常工作(ECHO 按钮向 PS 写入正确的字母),但 ComboBox 由单个字母填充(见图)。有人可以向我解释我做错了什么吗?

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Test' 
$form.Size = New-Object System.Drawing.Size(280,400) 
$form.StartPosition = 'Manual'
$form.Location      = '10,10'
$form.Topmost = $true  

$button = New-Object System.Windows.Forms.Button
$button.Size = New-Object System.Drawing.Size(50,20)
$button.Location = New-Object System.Drawing.Size(20,20)
$button.Text = "ECHO"
$button.Add_Click($button_click)

$comboBox1 = New-Object System.Windows.Forms.ComboBox 
$comboBox1.Location = New-Object System.Drawing.Point(80, 55) 
$comboBox1.Size = New-Object System.Drawing.Size(98, 10) 
$ComboBox1.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;

$AllLetters = 67..90 | ForEach-Object {[char]$_ + ":"}
$UsedLetters = Get-WmiObject Win32_LogicalDisk | Select -Expand DeviceID
$FreeLetters = $AllLetters | Where-Object {$UsedLetters -notcontains $_}
ForEach ($Letter in $FreeLetters) { $comboBox1.Items.Add($Drives) }



$button_click = {Write-Host ($FreeLetters)}                   

$form.Controls.AddRange(@($button, $combobox1))
$form.ShowDialog()

【问题讨论】:

  • $comboBox1.Items.Add($Drives) 更改为$comboBox1.Items.Add($Letter)
  • 是的!就是这样。谢谢

标签: arrays powershell foreach combobox


【解决方案1】:

在这一行:

ForEach ($Letter in $FreeLetters) { $comboBox1.Items.Add($Drives) }

$Drives 更改为$Letter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    相关资源
    最近更新 更多