Results 1 to 19 of 19

Thread: [RESOLVED] Problem with Color.FromName

Threaded View

  1. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [RESOLVED] Problem with Color.FromName

    If you wanted to give a color preview for your color selection process, you could use an extended combobox...

    Code:
    Public Class colorComboBox
        Inherits ComboBox
    
        Public Sub New()
            Me.DropDownStyle = ComboBoxStyle.DropDownList
            Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
            MyBase.Items.Clear()
            MyBase.Items.AddRange([Enum].GetNames(GetType(KnownColor)).OrderBy(Function(n) Color.FromName(n).GetHue).ToArray)
        End Sub
    
        Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
            Dim c As Color = Color.Empty
            If e.Index <> -1 Then
                c = Color.FromName(MyBase.Items(e.Index).ToString)
            End If
            If (e.State And DrawItemState.ComboBoxEdit) = DrawItemState.ComboBoxEdit Then
                ' Draw the contents of the edit area of the combo box.
                e.Graphics.FillRectangle(New SolidBrush(c), New Rectangle(4, e.Bounds.Top + 1, 16, 12))
                e.Graphics.DrawRectangle(Pens.Black, New Rectangle(4, e.Bounds.Top + 1, 16, 12))
                e.Graphics.DrawString(If(c.Name = "0", "Empty", c.Name), Me.Font, Brushes.Black, 22, e.Bounds.Top + 1)
            Else
                ' Draw the contents of an item in the drop down list.
                e.Graphics.FillRectangle(New SolidBrush(c), New Rectangle(2, e.Bounds.Top + 2, 16, 12))
                e.Graphics.DrawRectangle(Pens.Black, New Rectangle(2, e.Bounds.Top + 2, 16, 12))
                e.Graphics.DrawString(c.Name, Me.Font, Brushes.Black, 20, e.Bounds.Top + 2)
                ' Draw the focus rectangle.
                e.DrawFocusRectangle()
            End If
            MyBase.OnDrawItem(e)
        End Sub
    
        Public Function getSelectedColor() As Color
            If MyBase.SelectedIndex > -1 Then
                Return Color.FromName(MyBase.Text)
            Else
                Return Color.Transparent
            End If
        End Function
    
    End Class
    Name:  222.png
Views: 1414
Size:  2.5 KB
    Last edited by .paul.; Sep 26th, 2020 at 03:09 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width