[RESOLVED] Sorting colors by shade (or like it shows in the property window)
i am adding a color list in a combo box....using the code below.
My question is..
is there a way to sort it the same why the have it listed in the properties window? so the colors are grouped? Thanks!
Code:
'in form load
Dim ColorName As String
For Each ColorName In System.Enum.GetNames(GetType(System.Drawing.KnownColor))
Dim Syscolor As Color = Color.FromName(ColorName)
If Not Syscolor.IsSystemColor Then
cboColor.Items.Add(Color.FromName(ColorName))
End If
Next
'-----
Protected Sub cboColor_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles cboColor.MeasureItem
e.ItemHeight = 40
End Sub
Protected Sub cboColor_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cboColor.DrawItem
If e.Index < 0 Then
e.DrawBackground()
e.DrawFocusRectangle()
Exit Sub
End If
Dim CurrentColor As Color = CType(cboColor.Items(e.Index), Color)
Dim SizeRect As Rectangle = New Rectangle(2, e.Bounds.Top + 2, 40, e.Bounds.Height - 6)
Dim ComboBrush As Brush
e.DrawBackground()
e.DrawFocusRectangle()
If e.State = Windows.Forms.DrawItemState.Selected Then
ComboBrush = Brushes.White
Else
ComboBrush = Brushes.Black
End If
e.Graphics.DrawRectangle(New Pen(CurrentColor), SizeRect)
e.Graphics.FillRectangle(New SolidBrush(CurrentColor), SizeRect)
e.Graphics.DrawString(CurrentColor.Name, cboColor.Font, ComboBrush, e.Bounds.Height + 20, ((e.Bounds.Height - cboColor.Font.Height) \ 2) + e.Bounds.Top)
End Sub
Re: Sorting colors by shade (or like it shows in the property window)
Well obviously it's possible but you'll need to do some research into how it's done that way in the properties window which means finding your way round RGB values. I don't think there's any object available that does this automatically so you'll also need to learn how to create a custom comparer (something I've never really got my head round) as well.
Re: Sorting colors by shade (or like it shows in the property window)
try this:
Code:
'in form load
Dim ColorName As String
For Each ColorName In System.Enum.GetNames(GetType(System.Drawing.KnownColor)).OrderBy(Function(kc) Color.FromName(kc).GetHue)
Dim Syscolor As Color = Color.FromName(ColorName)
If Not Syscolor.IsSystemColor Then
cboColor.Items.Add(Color.FromName(ColorName))
End If
Next
edit: you need to sort by HSB (Hue, Saturation, Brightness). my example sorts by Hue
Re: Sorting colors by shade (or like it shows in the property window)
Paul... that is pure genius! PERFECTION!
I looked all over the net and did not find ONE simple solution!
Thanks!
Re: [RESOLVED] Sorting colors by shade (or like it shows in the property window)
DONE!
hi, i have the same problem, but couldnt integrate this solution to my code.
My code to list colors:
Private Sub BindWebColors()
'binding combobox with color values
'assign combobox drawmode
cmbWebColor.DrawMode = DrawMode.OwnerDrawFixed
cmbWebColor.ItemHeight = 20
Dim colType As Type = GetType(System.Drawing.Color)
For Each prop As PropertyInfo In colType.GetProperties()
If prop.PropertyType Is GetType(System.Drawing.Color) Then
cmbWebColor.Items.Add(prop.Name)
End If
Next
End Sub
how can i modify it to sort by shade? Thanks in advance
Re: Sorting colors by shade (or like it shows in the property window)
Paul, I am using Visual Studio 2017 and cannot get this code to work. Is there somewhere I can learn about using the system.enum.getnames with orderby?
1 Attachment(s)
Re: Sorting colors by shade (or like it shows in the property window)
Quote:
Originally Posted by
LuvWhiteSand
Paul, I am using Visual Studio 2017 and cannot get this code to work. Is there somewhere I can learn about using the system.enum.getnames with orderby?
here's a working example... Attachment 154917