我刚刚了解了Powershell GUI编程。以下代码可以成功执行,但是执行的结果不是列表中项目的文本。
像这样:
@ {Name = a1;状态=真}
@ {Name = a2;状态=假}
我需要直接输出项目的文本。
a1
a2
我尝试了很多方法,但没有结果 预先感谢
function Show-7_psf {
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$buttonOk = New-Object 'System.Windows.Forms.Button'
$checkedlistbox1 = New-Object 'System.Windows.Forms.CheckedListBox'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
$csvtxt = @'
Name,Status
a1,True
a2,False
a3,False
'@
$form1_Load = {
$csv = ConvertFrom-Csv $csvtxt
$checkedlistbox1.DataSource = [System.Collections.ArrayList]$csv
$checkedlistbox1.DisplayMember = 'Name'
for ($i = 0; $i -lt $checkedlistbox1.Items.Count; $i++)
{
$state = if ($checkedlistbox1.Items[$i].Status -eq 'True') { $true }
else { $false }
$checkedlistbox1.SetItemChecked($i, $state)
}
}
$buttonOk_Click = {
$checkedlistbox1.CheckedItems | Write-Host
}
$Form_StateCorrection_Load=
{
#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
$Form_Cleanup_FormClosed=
{
#Remove all event handlers from the controls
try
{
$buttonOk.remove_Click($buttonOk_Click)
$form1.remove_Load($form1_Load)
$form1.remove_Load($Form_StateCorrection_Load)
$form1.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
}
$form1.SuspendLayout()
$form1.Controls.Add($buttonOk)
$form1.Controls.Add($checkedlistbox1)
$form1.AutoScaleDimensions = '6, 13'
$form1.AutoScaleMode = 'Font'
$form1.ClientSize = '482, 262'
$form1.Name = 'form1'
$form1.StartPosition = 'CenterScreen'
$form1.Text = 'Form'
$form1.add_Load($form1_Load)
$buttonOk.Location = '357, 199'
$buttonOk.Name = 'buttonOk'
$buttonOk.Size = '67, 25'
$buttonOk.TabIndex = 1
$buttonOk.Text = 'OK'
$buttonOk.UseCompatibleTextRendering = $True
$buttonOk.UseVisualStyleBackColor = $True
$buttonOk.add_Click($buttonOk_Click)
$checkedlistbox1.CheckOnClick = $True
$checkedlistbox1.FormattingEnabled = $True
$checkedlistbox1.Location = '49, 30'
$checkedlistbox1.Name = 'checkedlistbox1'
$checkedlistbox1.Size = '177, 184'
$checkedlistbox1.TabIndex = 0
$checkedlistbox1.UseCompatibleTextRendering = $True
$form1.ResumeLayout()
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $form1.ShowDialog()
}
Show-7_psf | Out-Null
答案 0 :(得分:1)
更改此行
$checkedlistbox1.CheckedItems | Write-Host
收件人
$checkedlistbox1.CheckedItems.Name | Write-Host
这是因为您正在调用$csv
数组中的对象,但没有选择Property
,所以您将返回数组中的整个对象。
答案 1 :(得分:0)
如果您需要任何其他属性,请替换为名称
$buttonOk_Click = {
$checkedlistbox1.CheckedItems | Where-Object { $_."Name" | Write-Host }
}