-
Apr 14th, 2013, 12:16 PM
#1
[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
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Apr 14th, 2013, 12:49 PM
#2
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.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 14th, 2013, 12:52 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 28th, 2017, 02:01 PM
#4
New Member
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?
-
Dec 31st, 2017, 08:35 PM
#5
Re: Sorting colors by shade (or like it shows in the property window)
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... WindowsApp1.zip
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 14th, 2013, 12:55 PM
#6
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!
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Oct 4th, 2016, 03:04 AM
#7
New Member
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
Last edited by Ferrentino; Oct 4th, 2016 at 05:52 AM.
Reason: done
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|