PowerShell Combobox using a Custom DrawMode

Related Posts
2 Comments
futuremotion

FINALLY!

I have been searching high and low on how to customize the colors and theme of the winforms DropDownList ComboBox. I’ve poured through countless Stack Overflow posts and various forums and nothing really worked.

The key was:
$e.Graphics.FillRectangle($BackgroundColorBrush, $e.Bounds)

And omitting:
$e.DrawBackground()

Thank you so much for this post.

I have one last question.

When I first launch the application, the selected item (index 0) has the “WhiteSmoke” background. And also, after I choose an item from my ComboBox and the list collapses again, the “WhiteSmoke” background color remains.

Is there any way to revert back to a white background color once an item is chosen? Is there a way to set a default color (white) as a background when the application first launches?

I made a short screen recording to demonstrate what I mean:
https://streamable.com/eh16cs

Thanks again for the great write up and code.

Hi. You probably want to check the state of the combobox. Maybe replace line 58 with:

$backgroundColor = if(
(($e.State -band [System.Windows.Forms.DrawItemState]::Selected) -eq [System.Windows.Forms.DrawItemState]::Selected) -and
(($e.State -band [System.Windows.Forms.DrawItemState]::Focus) -eq [System.Windows.Forms.DrawItemState]::Focus) -and
(!(($e.State -band [System.Windows.Forms.DrawItemState]::ComboBoxEdit) -eq [System.Windows.Forms.DrawItemState]::ComboBoxEdit))
){

Leave a Reply
Send us a message

Send your message any time you want.

Our usual reply time: 2 Business Hours
Send us a message

Have a question? Send us a message.

Our usual reply time: 2 Business Hours