Reputation: 103
Here's what I have so far:
foreach($group in $userBGroups.Items) {
if($_.State -eq 'Selected') {
try {
$brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::LightBlue)
$_.Graphics.FillRectangle($brush, $_.bounds)
} finally {
$brush.Dispose()
}
} elseif($group -eq $lbItem) {
try {
$brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::LightGreen)
$_.Graphics.FillRectangle($brush, $_.Bounds)
} finally {
$brush.Dispose()
}
}
}
This works for the most part aside from the case where you want to select an item which is already colored green, it seems like it must be selecting it for a brief second then immediately it reverts to green. I'm lost as to what could be causing this behavior.
I do know that if I add "or" statements to the 'Selected' logic to look for NoAccelerator I do end up coloring all rows light blue and then when I click on them they either highlight a darker blue or the light green if they would have been that color without NoAccelerator.
Upvotes: 1
Views: 1698
Reputation: 103
So I managed to figure out a solution to my problem. I modified my DrawItem event to look like the following:
$userBGroups_DrawItem=[System.Windows.Forms.DrawItemEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DrawItemEventArgs]
if($userBGroups.Items.Count -eq 0) {return}
$_.DrawBackground()
$lbItem = $userBGroups.Items[$_.Index]
$itemBGColor = [System.Drawing.Color]::White
if($userBGroups.SelectedItems.Contains($lbItem)) {
$itemBGColor = [System.Drawing.Color]::LightBlue
} else {
if($userBGroups.Items -contains $lbItem) {
$itemBGColor = [System.Drawing.Color]::LightGreen
} else {
$itemBGColor = [System.Drawing.Color]::White
}
}
try {
$brush = New-Object System.Drawing.SolidBrush($itemBGColor)
$_.Graphics.FillRectangle($brush, $_.Bounds)
} finally {
$brush.Dispose
}
$_.Graphics.DrawString($lbItem, $_.Font, [System.Drawing.SystemBrushes]::ControlText, (New-Object System.Drawing.PointF($_.Bounds.X, $_.Bounds.Y)))
}
The key was that this code worked but was missing something. Whenever I selected an item then selected a different item I would still keep the previous selection. To resolve this I made the following selection changed event:
$userBGroups_SelectedValueChanged={
$userBGroups.Refresh()
}
It causes a flashing to occur after selecting an item when it redraws but is workable until I can figure out if there's a way to resolve that.
Upvotes: 1