[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

Visual Basic 2008. color dropdown menu

How do I make a color choosing dropdown just like the color dropdown in properties window?

I don't want the color dialog but a color dropdown.

Thanks!
0
NerdsOfTech
Asked:
NerdsOfTech
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is one version:
Imports System.Reflection
Public Class Form1
 
    Private KnownColors As New List(Of KnownColor)
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each C As Color In System.ComponentModel.TypeDescriptor.GetConverter(GetType(Color)).GetStandardValues
            If Not C.IsSystemColor Then
                KnownColors.Add(C.ToKnownColor)
            End If
        Next
        ComboBox1.DataSource = KnownColors
        ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
    End Sub
 
    Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
        Static width As Integer = 15
        e.DrawBackground()
        Dim KC As KnownColor = KnownColors(e.Index)
        Dim rc As New Rectangle(e.Bounds.X + 3, e.Bounds.Y + 3, width, e.Bounds.Height - 6)
        Using B As New SolidBrush(Color.FromKnownColor(KC))
            e.Graphics.FillRectangle(B, rc)
            e.Graphics.DrawRectangle(Pens.Black, rc)
        End Using
        e.Graphics.DrawString(KC.ToString, ComboBox1.Font, Brushes.Black, e.Bounds.X + (width + 6), e.Bounds.Y)
    End Sub
 
    Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
        If ComboBox1.SelectedIndex <> -1 Then
            Dim kc As KnownColor = ComboBox1.SelectedValue
            Me.BackColor = Color.FromKnownColor(kc)
        End If
    End Sub
 
End Class

Open in new window

ColorDropDown.jpg
0
 
NerdsOfTechTechnology ScientistAuthor Commented:
Another superb display of knowledge! Thank you Idle_Mind.

I will be asking another related question about this type of control in the future. I will post here to notify you.

=NerdsOfTech
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now