[RESOLVED] Sorting colors by shade (or like it shows in the property window)-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] Sorting colors by shade (or like it shows in the property window)

  1. #1

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,389

    Resolved [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"

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    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!

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,647

    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

  4. #4

    Thread Starter
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,389

    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"

  5. #5
    New Member
    Join Date
    Oct 2016
    Posts
    4

    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

  6. #6
    New Member
    Join Date
    Jun 2017
    Posts
    1

    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?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,647

    Re: Sorting colors by shade (or like it shows in the property window)

    Quote Originally Posted by LuvWhiteSand View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width