winforms 在textBox WindowsForm中输入数组对象

ih99xse1  于 2023-04-12  发布在  Windows
关注(0)|答案(1)|浏览(129)

我尝试输入数组对象 $WinRoles(从远程主机获取WindowsFeature)。需要将 $WinRoles.DisplayName$WinRoles.InstallState 作为表插入到文本框 $TextBoxInstalledRolesFeatures
下面我的代码(这是出现此问题的代码部分),我缺少哪里?

<#------======== Label Installed Roles&Features ========------#>
$LabelInstalledRolesFeatures = New-Object System.Windows.Forms.Label
$LabelInstalledRolesFeatures.Location = '810,10'
$LabelInstalledRolesFeatures.AutoSize = $true
$LabelInstalledRolesFeatures.Text = 'Installed Roles and Features'
$CommonForm.Controls.Add($LabelInstalledRolesFeatures)
<#------======== Label Installed Roles&Features ========------#>
<#------======== TextBox Installed Roles&Features ========------#>
$TextBoxInstalledRolesFeatures = New-Object System.Windows.Forms.ListBox
$TextBoxInstalledRolesFeatures.Location = '810,30'
$TextBoxInstalledRolesFeatures.Size = '250,316'
$TextBoxInstalledRolesFeatures.Text = ''
$CommonForm.Controls.Add($TextBoxInstalledRolesFeatures)
<#------======== TextBox Installed Roles&Features ========------#>
<#------======== Button Show Roles&Features ========------#>
$ButtonShowRolesFeatures = New-Object System.Windows.Forms.Button
$ButtonShowRolesFeatures.Location = '985, 5'
$ButtonShowRolesFeatures.Text = 'Show'
$ButtonShowRolesFeatures.AutoSize = $true
$ButtonShowRolesFeatures.add_Click({

        $SelectedServer = $ListboxListeServers.SelectedItem
        $Session = New-PSSession -ComputerName $SelectedServer
        $WinRoles = Invoke-Command -Session $Session -ScriptBlock {
                        Get-WindowsFeature | where Installed -eq $true
                    }
        [array]$AllRoles = $WinRoles | select DisplayName, InstallState
        $TextBoxInstalledRolesFeatures.Text = $AllRoles
})
$CommonForm.Controls.Add($ButtonShowRolesFeatures)
<#------======== Button Show Roles&Features ========------#>
mitkmikd

mitkmikd1#

你应该使用DataGridView而不是ListBox,就像评论中推荐的Mathias一样。这个实现并不复杂,是你用例的理想控件。我的笔记本电脑里没有Get-WindowsFeature,但作为一个例子,这里有一个使用Get-Process作为网格数据源的实现:

Add-Type -AssemblyName System.Windows.Forms

$CommonForm = [System.Windows.Forms.Form]@{
    Size          = '1800, 900'
    StartPosition = 'CenterScreen'
}

$ListboxListeServers = [System.Windows.Forms.ListBox]@{
    Location = '30, 40'
    Size     = '200, 800'
}
$ListboxListeServers.Items.AddRange(@((Get-Process).ProcessName | Sort-Object -Unique))
$CommonForm.Controls.Add($ListboxListeServers)

$dgvInstalledRolesFeatures = [System.Windows.Forms.DataGridView]@{
    Location = '240, 40'
    Size     = '1400, 800'
}
$CommonForm.Controls.Add($dgvInstalledRolesFeatures)

$ButtonShowRolesFeatures = [System.Windows.Forms.Button]@{
    Location = '985, 5'
    Text     = 'Show'
    AutoSize = $true
}

$processlist = [System.Collections.ArrayList]::new()
$ButtonShowRolesFeatures.add_Click({
    $processlist.AddRange(@(Get-Process $ListboxListeServers.SelectedItem))
    $dgvInstalledRolesFeatures.DataSource = $null
    $dgvInstalledRolesFeatures.DataSource = $processlist
})
$CommonForm.Controls.Add($ButtonShowRolesFeatures)

$CommonForm.ShowDialog()

相关问题