Winforms: How can I change the background color of a ComboBox with DropDownStyle set to DropDownList?

I am creating a Winforms GUI wrapper around a script that I use to separate audio components. Here’s how it looks before, and after:

The first example in the image above uses ComboBox components where the DropDownStyle is set to DropDown. The second is set to DropDownList. I need DropDownList to prevent the user from typing in invalid data. I don’t want the user to be able to type input into the ComboBoxes.

Unfortunately, as you can see - when I change to DropDownList, the style of the ComboBox changes to a darker gray that I don’t want.

I can’t seem to change this no matter what I do. I did some google-fu and found a few related posts:

So I tried creating an event handler for the combobox, and then added this code:

$COMBO_SeperationModel_DrawItem = {
    [System.Object] $sender, 
    [System.Windows.Forms.DrawItemEventArgs] $e

    $wshell = New-Object -ComObject Wscript.Shell

    $Brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::Aqua)
    $e.Graphics.FillRectangle($Brush, $e.Bounds)
    $e.Graphics.DrawString($COMBO_SeperationModel.Items[0].ToString(), $e.Font, $Brush, $e.Bounds, [System.Drawing.StringFormat]::GenericDefault)


I added the popup just to see if the function itself was firing, but it’s not. So I’m kind of at a loss.

Do any of you here know how to implement a function/event handler that will manually draw the border and paint the background color?

Any help greatly appreciated!


Ok, an update. I managed to get the event handler to fire and came up with this code that works-ish:

$COMBO_SeparationModel_DrawItem = {

    [System.Object] $sender, 
    [System.Windows.Forms.DrawItemEventArgs] $e

    $index = ($e.Index -ge 0) ? $e.Index : 0
    $DrawItemStateSelected = [System.Windows.Forms.DrawItemState]::Selected
    $HighlightBrush = [System.Drawing.SystemBrushes]::HighlightText
    $BlackBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::Black)
    $GrayBrush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::DarkGray)

    if( ($e.State -gt 0) -and ($DrawItemStateSelected -gt 0) ){
        $Brush = $HighlightBrush
        $Brush = $BlackBrush
    #$Brush = ( ($e.State & $DrawItemStateSelected) -gt 0 ) ? $HighlightBrush : $BlackBrush
    $e.Graphics.FillRectangle($HighlightBrush, $e.Bounds)
    $ptx = $e.Bounds.X
    $pty = $e.Bounds.Y
    $ptw = $e.Bounds.Width
    $pth = $e.Bounds.Height
    $ptl = $e.Bounds.Left
    $ptt = $e.Bounds.Top

    $rect = [System.Drawing.RectangleF]::FromLTRB($ptl, $ptt, $ptw, $pth)

    $e.Graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::AntiAliasGridFit
    $e.Graphics.DrawString($COMBO_SeperationModel.Items[$index].ToString(), $e.Font, $BlackBrush, $rect, [System.Drawing.StringFormat]::GenericDefault)


$DemucsForm_Load = {
     #$wshell = New-Object -ComObject Wscript.Shell

. (Join-Path $PSScriptRoot 'DemucsForm.designer.ps1')

Add-Type -AssemblyName PresentationCore,PresentationFramework


Everything works great except that only the first two combobox entries are rendered black. The remaining two are either transparent or white and I have no clue why. :frowning:

Here’s a video:

Can anyone see an issue with my code that would be causing this to happen?