提问人:Catcher Rem 提问时间:4/4/2023 最后编辑:Santiago SquarzonCatcher Rem 更新时间:4/5/2023 访问量:85
textBox WindowsForm 中的输入数组对象
Input array object in textBox WindowsForm
问:
我尝试输入数组对象$WinRoles(来自远程主机的 Get-WindowsFeature)。需要在文本框$TextBoxInstalledRolesFeatures中插入 $WinRoles.DisplayName 和 $WinRoles.InstallState 作为表
我的代码(这是发生此问题的代码部分)。我在哪里错过了?
<#------======== 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 ========------#>
答:
0赞
Santiago Squarzon
4/5/2023
#1
您应该使用 DataGridView,而不是 Mathias 在注释中建议的 DataGridView
。实现不是很复杂,是您的用例的理想控制。我的笔记本电脑中没有,但作为一个例子,这里有一个用作网格数据源的实现:ListBox
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()
评论
1赞
Doofus
4/5/2023
我以前从未见过这种定义窗体控件的方法,它省去了按钮名称变量的大量重复使用。谢谢你。应该使手动编码人员更快地构建新的 WinForm。
0赞
Catcher Rem
4/5/2023
@SantiagoSquarzon 谢谢!这是需要的!和工作!
0赞
Catcher Rem
4/5/2023
如何使在请求新数据时添加新行,而不是替换当前行?
0赞
Santiago Squarzon
4/5/2023
@CatcherRem我很高兴它很有帮助。请参阅更新注册表。从本质上讲,将 设置为 null,然后将 ArrayList 重新分配给它DataSource
评论
$WinRoles.GetType() =
尝试将对象分配给文本框将导致其字符串化DataGrid
而不是ListBox
$TextBoxInstalledRolesFeatures.Text = $AllRoles
应该是主要问题,但我同意 Mathias 的观点,您应该为此使用 DGV$TextBoxInstalledRolesFeatures.Items.AddRange($AllRoles)