I've started learning Powershell and am now stuck after spending several hours on a problem I can find solutions for in multiple languages except Powershell.
I need to place a check against each item in a CheckedListBox that matches any of the values in a semi-colon delimited string named $MLBSVar_SelectedPackages
. (eg. $MLBSVar_SelectedPackages = 'packageA;packageB;packageC;'
) and so on.
I've come up with this line but it doesn't yet work. Please can you help me?
if ($MLBSVar_SelectedPackages -ne $null) {
ForEach ($PackageName in $MLBSVar_SelectedPackages) {
ForEach ($item in $clb_SC_AvailablePackages.Items) {
if ($item -eq $PackageName) {
$clb_SC_AvailablePackages.Item($PackageName).Checked = $true
}
}
}
}
I've also tried .SetItemCheckState([System.Windows.Forms.CheckState]::Checked)
in place of .Checked
. The (one) issue seems to be getting a handle on the list item in the final section as it seems to be passed as a string rather than object? I have a VBS background and would really appreciate the assistance.
I think what you're looking for is something like the following code. You can use the
SetItemChecked()
method of theCheckedListBox
class to check an item at a particular index. I see that you have attempted to useSetItemCheckState()
, but did not make mention ofSetItemChecked()
.After running this code, you should be presented with a dialog that looks similar to the following screenshot.