visusys
February 18, 2023, 3:17am
1
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:
.net, winforms
Before: It is Dropdown, inside comboBox, it is white color background.
Now: It is Dropdown List, the color inside comboBox turns to grey.
How to change it back to white? I did not find it in property.
Thanks.
So I tried creating an event handler for the combobox, and then added this code:
$COMBO_SeperationModel_DrawItem = {
param(
[System.Object] $sender,
[System.Windows.Forms.DrawItemEventArgs] $e
)
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Test",0,"Done",0x1)
$Brush = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::Aqua)
$e.Graphics.FillRectangle($Brush, $e.Bounds)
$e.DrawBackground()
$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!
Jay
visusys
February 18, 2023, 4:30am
2
Ok, an update. I managed to get the event handler to fire and came up with this code that works-ish:
$COMBO_SeparationModel_DrawItem = {
param(
[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
}else{
$Brush = $BlackBrush
}
#$Brush = ( ($e.State & $DrawItemStateSelected) -gt 0 ) ? $HighlightBrush : $BlackBrush
$e.Graphics.FillRectangle($HighlightBrush, $e.Bounds)
$e.DrawBackground()
$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)
$e.DrawFocusRectangle()
}
$DemucsForm_Load = {
#$wshell = New-Object -ComObject Wscript.Shell
#$wshell.Popup(($PSVersionTable.PSVersion),0,"Done",0x1)
}
. (Join-Path $PSScriptRoot 'DemucsForm.designer.ps1')
Add-Type -AssemblyName PresentationCore,PresentationFramework
[System.Windows.Forms.Application]::EnableVisualStyles()
$FORM_VSYSDemucsUI.ShowDialog()
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.
Here’s a video:
Can anyone see an issue with my code that would be causing this to happen?