Here's the code I have so far below which is populated with an array ([System.Drawing.Color] | Get-Member -Static -MemberType Properties).name.
I was trying to see if there was any way to set the BackColor property of each item to match that of the same array element of colours but there's none.
Ideally, I'd like to get something like this with a little swatch of the colours to the left of the name.
EDIT: I've since found that you can add an event handler for the control but I'm still unsure how to change each item to show a little swatch. I've updated the code below as the other coded wasn't that close.
The code I have so far:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.Font = [System.Drawing.Font]::New("Segoe UI", 10, [System.Drawing.FontStyle]::Regular) # Bold, Italic, Regular, Strikeout or Underline
$Form.StartPosition = 'CenterScreen'
$Form.Text = 'How do I Change Each Items Colour to Match Their Name?'
$Form.Width = 500
$Form.Height = 69
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle # 1 A fixed, single-line border
$Form.SizeGripStyle = [System.Windows.Forms.SizeGripStyle]::Hide # 2 The sizing grip is hidden.
$combo = New-Object System.Windows.Forms.ComboBox
$combo.DrawMode = 'OwnerDrawFixed'
$combo.FlatStyle = [System.Windows.Forms.FlatStyle]::System
$combo.Font = [System.Drawing.Font]::New("Segoe UI", 14, [System.Drawing.FontStyle]::Regular) # Bold, Italic, Regular, Strikeout or Underline
$combo.Width = $Form.Width-1-$combo.Height+($combo.Margin.Left+$combo.Margin.Right)
$combo.AutoCompleteMode = 'SuggestAppend' # Enables searching but not mid string. Need to find a solution for this.
$combo.AutoCompleteSource = 'ListItems'
$combo.AutoSize = $true
$combo.DropDownStyle = 'DropDownList'
$combo.ItemHeight = 24
$colorNamesArr = ([System.Drawing.Color] | Get-Member -Static -MemberType Properties).name # Here's the array of colour that are to get added to the ComboBox.
$combo.Items.AddRange($colorNamesArr) # Add the array to the ComboBox.
$Form.Controls.Add($combo)
# HERE'S WHERE I NEED HELP.
# How do I change the colours of the ComboBox items to match that of their name.
$combo.Add_DrawItem({param($sender,$e)
$text = "-- Select Color --"
if ($e.Index -gt -1){
$text = $sender.GetItemText($sender.Items[$e.Index])
}
$e.DrawBackground()
[System.Windows.Forms.TextRenderer]::DrawText($e.Graphics, $text, $combo.Font, $e.Bounds, $e.ForeColor, [System.Windows.Forms.TextFormatFlags]::Default)
})
$Form.ShowDialog() | Out-Null
$Form.Dispose()
