Results 1 to 19 of 19

Thread: [RESOLVED] Problem with Color.FromName

Hybrid View

  1. #1
    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: 1413
Size:  2.5 KB
    Last edited by .paul.; Sep 26th, 2020 at 03:09 PM.

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: [RESOLVED] Problem with Color.FromName

    Quote Originally Posted by .paul. View Post
    If you wanted to give a color preview for your color selection process, you could use an extended combobox...
    That's a nice tips, thanks (need to spread some rep before being able to give some to you).

    I wonder if I can replace the colored rectangle with an image...?? I will try that as a good exercise.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

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

    Re: [RESOLVED] Problem with Color.FromName

    Quote Originally Posted by Delaney View Post
    I wonder if I can replace the colored rectangle with an image...?? I will try that as a good exercise.
    You can draw anything you choose in the DrawItem event, but it helps to have something that relates to the item, as in this case...

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,130

    Re: [RESOLVED] Problem with Color.FromName

    Quote Originally Posted by .paul. View Post
    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: 1413
Size:  2.5 KB
    nice one
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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