Home » Category » Microsoft Visual Basic

Microsoft Visual Basic: Colour Combo Box

100| Sat, 29 Dec 2007 23:31:00 GMT| xplosiv_1| Comments (14)
Hi,

How do i get a combo box to display system colours?

As i am using it so the user can select the background colour for a graph.

I have tried colourcb.text = color() but this produces an error any ideas?

Andy

Keywords & Tags: colour, combo, box, microsoft, visual basic, vb

URL: http://www.programmerbase.com/visual-basic/80728/
 
«« Prev - Next »» 14 helpful answers below.
Why not use a ColorDialog ?

dave299 | Wed, 05 Sep 2007 10:33:00 GMT |

Sorry

i am new to VB so don't know all the default dialog boxes available.

Is there a list with all the dialogue boxes some where?

How would i use the colour dialogue box? just place a button

the type = colour dialogue

Andy

xplosiv_1 | Wed, 05 Sep 2007 10:34:00 GMT |

Just enter dialogs into the Help search and you should find them all.

Basic use as follows:

Dim CD As New ColorDialog
Dim SelectedColor As Color
If CD.ShowDialog() = Windows.Forms.DialogResult.OK Then
SelectedColor = CD.Color
End If

dave299 | Wed, 05 Sep 2007 10:35:00 GMT |

Change the background color of the form to the color the user selected in the color dialog..

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim cd As New ColorDialog

With cd

.AllowFullOpen = True

.Color = Me.BackColor

.FullOpen = True

.ShowDialog()

End With

Me.BackColor = cd.Color

End Sub

dman1 | Wed, 05 Sep 2007 10:36:00 GMT |

HI,

Is there a colour box which displays the list you get in visual sudio when you type

color. Aqua blue
Brick red
etc

?

Thanks,

ANdy

xplosiv_1 | Wed, 05 Sep 2007 10:37:00 GMT |

Not that I'm aware of but try this:

Public Class Form1
Dim Colours() As KnownColor
Dim F As New Font("Arial", 12, FontStyle.Bold)
Dim WithEvents CMBO As New ComboBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CMBO.SetBounds(50, 50, 200, 20)
CMBO.DrawMode = DrawMode.OwnerDrawFixed
CMBO.Font = F
Me.Controls.Add(CMBO)
Colours = DirectCast([Enum].GetValues(GetType(KnownColor)), KnownColor())
For Each C As KnownColor In Colours
CMBO.Items.Add([Enum].GetName(GetType(KnownColor), CInt(C)))
Next
End Sub
Private Sub CMBO_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles CMBO.DrawItem
e.DrawBackground()
Dim C As Color = Color.FromName(CMBO.Items(e.Index).ToString)
Dim B As New SolidBrush(C)
e.Graphics.FillRectangle(B, New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.Graphics.DrawString(CMBO.Items(e.Index).ToString, F, Brushes.Black, e.Bounds.X, e.Bounds.Y)
End Sub
Private Sub CMBO_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CMBO.SelectedIndexChanged
Me.Text = CMBO.SelectedItem.ToString
End Sub
End Class

dave299 | Wed, 05 Sep 2007 10:38:00 GMT |

^^^ Dave299 beat me to it, but I'll post my code anyway:



Public Class Form1

Private WithEvents zColorCombobox1 As New xxColorCombobox
Private WithEvents zColorCombobox2 As New xxColorCombobox
Private WithEvents zColorCombobox3 As New xxColorCombobox

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

zColorCombobox1.ColorsToShow = xxColorCombobox.KnownColorsToShow.SystemColorsOnly
zColorCombobox1.Dock = DockStyle.Top
Me.Controls.Add(zColorCombobox1)

zColorCombobox2.ColorsToShow = xxColorCombobox.KnownColorsToShow.NamedWebColorsOnly
zColorCombobox2.Dock = DockStyle.Bottom
Me.Controls.Add(zColorCombobox2)

zColorCombobox3.ColorsToShow = xxColorCombobox.KnownColorsToShow.All
zColorCombobox3.BackColor = Color.FromArgb(64, 64, 64)
zColorCombobox3.ForeColor = Color.White
zColorCombobox3.Font =
New Font("Tahoma", 8, FontStyle.Bold, GraphicsUnit.Point)
zColorCombobox3.Dock = DockStyle.Top
Me.Controls.Add(zColorCombobox3)

End Sub

Private Sub zColorComboboxes_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles zColorCombobox1.SelectedIndexChanged, zColorCombobox2.SelectedIndexChanged, zColorCombobox3.SelectedIndexChanged
Dim zColorCombobox As xxColorCombobox = sender
Me.BackColor = zColorCombobox.SelectedColor
End Sub
End
Class

Public Class xxColorCombobox
Inherits System.Windows.Forms.ComboBox

Public Enum KnownColorsToShow
All = 0
SystemColorsOnly = 1
NamedWebColorsOnly = 2
End Enum

Private zSelectedColor As Color = Color.AliceBlue
Private zColorsToShow As KnownColorsToShow = KnownColorsToShow.All
Private zIncludeKnownColor_Transparent As Boolean = False

Sub New()
With Me
.Parent = Nothing
.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
.DropDownStyle = ComboBoxStyle.DropDownList
End With
Me.ColorsToShow = KnownColorsToShow.All
End Sub

Private Sub xxColorCombobox_ParentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ParentChanged
Me.Update_ColorList()
End Sub

<System.ComponentModel.Category("Appearance")> Public Property SelectedColor() As Color
Get
Return zSelectedColor
End Get
Private Set(ByVal value As Color)
Try
zSelectedColor = value
Catch ex As Exception
End Try
End Set
End Property

<System.ComponentModel.Category("Appearance")> Public Property ColorsToShow() As KnownColorsToShow
Get
Return zColorsToShow
End Get
Set(ByVal value As KnownColorsToShow)
zColorsToShow = value
Update_ColorList()
End Set
End Property

<System.ComponentModel.Category("Behavior")> Public Property IncludeKnownColor_Transparent() As Boolean
Get
Return zIncludeKnownColor_Transparent
End Get
Set(ByVal value As Boolean)
zIncludeKnownColor_Transparent = value
End Set
End Property

Private Sub Update_ColorList()
Try
Me.Items.Clear()
Dim zArrayList As New ArrayList
zArrayList.Clear()
For Each zKnownColor As KnownColor In [Enum].GetValues(GetType(KnownColor))
If zKnownColor = KnownColor.Transparent AndAlso zIncludeKnownColor_Transparent = False Then Continue For
Select Case zColorsToShow
Case KnownColorsToShow.All
zArrayList.Add(zKnownColor.ToString)
Case KnownColorsToShow.NamedWebColorsOnly
If Color.FromKnownColor(zKnownColor).IsSystemColor = False Then
zArrayList.Add(zKnownColor.ToString)
End If
Case KnownColorsToShow.SystemColorsOnly
If Color.FromKnownColor(zKnownColor).IsSystemColor = True Then
zArrayList.Add(zKnownColor.ToString)
End If
End Select
Next
zArrayList.TrimToSize()
zArrayList.Sort()
Me.Items.AddRange(zArrayList.ToArray)
Me.SelectedIndex = 0
Catch ex As Exception
End Try
End Sub

Private Sub xxColorCombobox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
Try
e.DrawBackground()
Dim zColor As Color = Color.FromKnownColor([Enum].Parse(GetType(KnownColor), Me.Items(e.Index)))
Dim zColorName As String = Color.FromKnownColor([Enum].Parse(GetType(KnownColor), Me.Items(e.Index))).ToKnownColor.ToString
Dim zLGB As New Drawing2D.LinearGradientBrush(e.Bounds, Me.BackColor, zColor, Drawing2D.LinearGradientMode.Horizontal)
Dim zBrush As New SolidBrush(Me.ForeColor)
e.Graphics.FillRectangle(zLGB, e.Bounds)
e.Graphics.DrawString(zColorName,
Me.Font, zBrush, e.Bounds.X, e.Bounds.Y)
e.DrawFocusRectangle()
Catch ex As Exception
End Try
End Sub

Private Sub xxColorCombobox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SelectedIndexChanged
Try
If Me.SelectedItem Is Nothing Then Exit Sub
Me.SelectedColor = Color.FromKnownColor([Enum].Parse(GetType(KnownColor), Me.Items(Me.SelectedIndex)))
Catch ex As Exception
End Try
End Sub

Private Sub xxColorCombobox_SystemColorsChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SystemColorsChanged
Me.Update_ColorList()
End Sub
End
Class

nogchoco | Wed, 05 Sep 2007 10:39:00 GMT |

Very nice - I'd been trying to think of an elegant way to make sure the text would always be readable.

dave299 | Wed, 05 Sep 2007 10:40:00 GMT |

Dave299 wrote:
Very nice - I'd been trying to think of an elegant way to make sure the text would always be readable.

Thanks, Dave - I ? gradients, it's a cheap effect but they never go out of style

nogchoco | Wed, 05 Sep 2007 10:41:00 GMT |

nogChoco,

As i am rubbish at vb (new and all)

How would i make your code eqal my colourcbo?

As in: i created your project and it displays three combo box's all i need is to produce the result you get in the combobox at the bottom (that displays aliceblue) in my

colourcbo (

and be able to make selected colour variable equal to the selsect colour.

By the way, very nice code

Many Thanks,

Andy

xplosiv_1 | Wed, 05 Sep 2007 10:42:00 GMT |

Thanks - it's pretty much the same as Dave's code, though

The xxColorCombobox class that I created (under the Form1 Class code) can be used like a normal combobox. If you're using VB2005, then build or run the solution once, and then you should see 'xxColorCombobox' in the toolbox like the other controls (it will be in a section that is named after your project).

Or you can create it via code, like you would with a normal combobox. xxColorCombobox has a .SelectedColor property that gives you the color that is currently selected in the ColorCombobox:

Private aNewCombo as New xxColorCombobox
Private aColor as Color = aNewCombo.SelectedColor

And you can use the .ColorsToShow property to limit the colors to SystemColors or NamedWebColors only (or All of them). The SelectedIndexChanged event can be used to keep your color-variable up to date with the color you selected in the combobox:

Private Sub aNewCombo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles aNewCombo.SelectedIndexChanged

Dim zColorCombobox As xxColorCombobox = sender

aColor = zColorCombobox.SelectedColor

End Sub



EDIT: Hmmm... for some reason, it's showing the NamedWebColors twice. Gonna look into that.

nogchoco | Wed, 05 Sep 2007 10:43:00 GMT |

nogChoco,

Took me a few minutes to figure out how to 'dll build' it

to add to my toolbox. (versus just using it as a class.)

But man, that code rocks!

talldude | Wed, 05 Sep 2007 10:45:00 GMT |

Thanks, Tall Dude - note that there seems to be a little problem with it at the moment, though - I think the ArrayList is causing the list to double (so all entries appear twice), but haven't gotten to the bottom of it yet - i'll edit the fixed code into my earlier reply, as soon as I figure out where the doubling is happening.

EDIT: The list only doubles when the control is added at designtime, so I'm not sure I'll be able to figure it out

nogchoco | Wed, 05 Sep 2007 10:45:00 GMT |

Make this change to fix the code:

Private Sub Update_ColorList()

Try

Me.Items.Clear()

Dim zArrayList As New ArrayList(40)

zArrayList.Clear()

The Sub New() and the ColorsToShow() are

both calling Me.Update_ColorList when the

control is initialized and because zArrayList

already exists, the code is not waiting for another

new one to get initialized.

Edited comment:

Well it fixed it in one test. Now I don't know ....?

talldude | Wed, 05 Sep 2007 10:47:00 GMT |

Microsoft Visual Basic Hot Answers

Microsoft Visual Basic New questions

Microsoft Visual Basic Related Categories