|
-
Sep 26th, 2020, 01:28 AM
#1
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
Last edited by .paul.; Sep 26th, 2020 at 03:09 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 30th, 2020, 03:33 AM
#2
Re: [RESOLVED] Problem with Color.FromName
 Originally Posted by .paul.
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)
-
Sep 30th, 2020, 02:19 PM
#3
Re: [RESOLVED] Problem with Color.FromName
 Originally Posted by Delaney
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 30th, 2020, 03:38 AM
#4
Re: [RESOLVED] Problem with Color.FromName
 Originally Posted by .paul.
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

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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|