Color ComboBox In vb

Santanu.Das

Now I am going through a project with colors. And then I think that if I try to display color values with their names in a simple combobox, it could be amazing. I tried and did it and it is looking like
Capture3.png

And
Capture4.png

And finally the project looks like
Capture2.PNG

In that codes, firstly I tried to bind combobox with colors.

 Private Sub BindWebColors()
        'binding combobox with color vaues

        '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

When a color value is added to the combobox, it fires the DrawItem event.

Private Sub cmbWebColor_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles cmbWebColor.DrawItem
        'Drawing the itemsinto the combobox.
        'Every time it fires when an item is drawn to the combobox.

        'exit if there is no item
        If e.Index = -1 Then
            Exit Sub
        End If


        'declare a colorbrush
        Dim colBrush As Brush = New SolidBrush(Color.FromName(DirectCast(cmbWebColor.Items(e.Index), String)))

        'Drawing rectangles for the color values
        e.Graphics.DrawRectangle(New Pen(Brushes.Black), e.Bounds.Left + 2, e.Bounds.Top + 2, 30, e.Bounds.Height - 5)
        e.Graphics.FillRectangle(colBrush, e.Bounds.Left + 3, e.Bounds.Top + 3, 29, e.Bounds.Height - 6)

        'Draw the name of the color
        e.Graphics.DrawString(DirectCast(cmbWebColor.Items(e.Index), String), cmbWebColor.Font, Brushes.Black, 35, e.Bounds.Top + 2)

    End Sub

Samely I did for the system colors

 Private Sub BindSysColors()
        'binding combobox with systemcolor vaues

        'assign combobox drawmode
        cmbSysColor.DrawMode = DrawMode.OwnerDrawFixed
        cmbSysColor.ItemHeight = 20


        Dim sysType As Type = GetType(System.Drawing.SystemColors)


        For Each prop As PropertyInfo In sysType.GetProperties()
            If prop.PropertyType Is GetType(System.Drawing.Color) Then
                cmbSysColor.Items.Add(prop.Name)
            End If
        Next
    End Sub

And draw the item for system colors

Private Sub cmbSysColor_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles cmbSysColor.DrawItem
        'Drawing the itemsinto the combobox.

        'exit if there is no item
        If e.Index = -1 Then
            Exit Sub
        End If


        'declare a colorbrush
        Dim sysBrush As Brush = New SolidBrush(Color.FromName(DirectCast(cmbSysColor.Items(e.Index), String)))

        'Drawing rectangles for the color values
        e.Graphics.DrawRectangle(New Pen(Brushes.Black), e.Bounds.Left + 2, e.Bounds.Top + 2, 30, e.Bounds.Height - 5)
        e.Graphics.FillRectangle(sysBrush, e.Bounds.Left + 3, e.Bounds.Top + 3, 29, e.Bounds.Height - 6)

        'Draw the name of the color
        e.Graphics.DrawString(DirectCast(cmbSysColor.Items(e.Index), String), cmbSysColor.Font, Brushes.Black, 35, e.Bounds.Top + 2)

    End Sub

Now you can call the SelectionChanged Event for the comboboxes to change the forecolor/backcolor of an object. Here I took a Label Control to change its Forecolor.

Private Sub cmbColors_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cmbWebColor.SelectedIndexChanged, cmbSysColor.SelectedIndexChanged

        Dim cmb As ComboBox = CType(sender, ComboBox)

        Label1.ForeColor = Color.FromName(cmb.Items(cmb.SelectedIndex))
End Sub

And it changes the Forecolor of the label.

The total code samples are follows.

3,588 Views
About the Author
Imports System
Imports System.Drawing
Imports System.Reflection
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Calling subprocedures
        Me.BindWebColors()
        Me.BindSysColors()


    End Sub

    Private Sub BindWebColors()
        'binding combobox with color vaues

        '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

    Private Sub BindSysColors()
        'binding combobox with systemcolor vaues

        'assign combobox drawmode
        cmbSysColor.DrawMode = DrawMode.OwnerDrawFixed
        cmbSysColor.ItemHeight = 20


        Dim sysType As Type = GetType(System.Drawing.SystemColors)


        For Each prop As PropertyInfo In sysType.GetProperties()
            If prop.PropertyType Is GetType(System.Drawing.Color) Then
                cmbSysColor.Items.Add(prop.Name)
            End If
        Next
    End Sub

    Private Sub cmbWebColor_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles cmbWebColor.DrawItem
        'Drawing the itemsinto the combobox.
        'Every time it fires when an item is drawn to the combobox.

        'exit if there is no item
        If e.Index = -1 Then
            Exit Sub
        End If


        'declare a colorbrush
        Dim colBrush As Brush = New SolidBrush(Color.FromName(DirectCast(cmbWebColor.Items(e.Index), String)))

        'Drawing rectangles for the color values
        e.Graphics.DrawRectangle(New Pen(Brushes.Black), e.Bounds.Left + 2, e.Bounds.Top + 2, 30, e.Bounds.Height - 5)
        e.Graphics.FillRectangle(colBrush, e.Bounds.Left + 3, e.Bounds.Top + 3, 29, e.Bounds.Height - 6)

        'Draw the name of the color
        e.Graphics.DrawString(DirectCast(cmbWebColor.Items(e.Index), String), cmbWebColor.Font, Brushes.Black, 35, e.Bounds.Top + 2)

    End Sub


    Private Sub cmbSysColor_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles cmbSysColor.DrawItem
        'Drawing the itemsinto the combobox.

        'exit if there is no item
        If e.Index = -1 Then
            Exit Sub
        End If


        'declare a colorbrush
        Dim sysBrush As Brush = New SolidBrush(Color.FromName(DirectCast(cmbSysColor.Items(e.Index), String)))

        'Drawing rectangles for the color values
        e.Graphics.DrawRectangle(New Pen(Brushes.Black), e.Bounds.Left + 2, e.Bounds.Top + 2, 30, e.Bounds.Height - 5)
        e.Graphics.FillRectangle(sysBrush, e.Bounds.Left + 3, e.Bounds.Top + 3, 29, e.Bounds.Height - 6)

        'Draw the name of the color
        e.Graphics.DrawString(DirectCast(cmbSysColor.Items(e.Index), String), cmbSysColor.Font, Brushes.Black, 35, e.Bounds.Top + 2)

    End Sub

    Private Sub cmbColors_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles cmbWebColor.SelectedIndexChanged, cmbSysColor.SelectedIndexChanged

        Dim cmb As ComboBox = CType(sender, ComboBox)

        Label1.ForeColor = Color.FromName(cmb.Items(cmb.SelectedIndex))
    End Sub
End Class
JerrimePatient 5 Junior Poster in Training

Nice Demo. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.