Я только что узнал о программировании графического интерфейса PowerShell. Следующий код может быть успешно выполнен, но результатом выполнения не является текст элемента в списке.
как это:
@ {Name = a1; Статус = True}
@ {Name = a2; Статус = False}
Мне нужно вывести текст элемента напрямую.
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