Solved

Visual Basic 2008 Color Dropdown Part 2

Posted on 2009-05-07
10
1,060 Views
Last Modified: 2013-11-27
Idle_Mind helped in creating a very nice color dropdown (Previous Linked Q). Now I am looking to get as close as possible to Visual Basic 2008 color dropdown control. How would I be able to create this?

Is it possible to have tabs in a dropdown like this or is MS using a special type of control?

Please include entire project code including form1 and a screenshot of the solution
color.jpg
webcolor.jpg
systemcolor.jpg
ColorDropDown.jpg
0
Comment
Question by:NerdsOfTech
  • 6
  • 4
10 Comments
 
LVL 19

Author Comment

by:NerdsOfTech
ID: 24331687
Is this dropdown just a small form (without boarders, close buttons, etc) with tabs that appears below the dropdown until the user unfocuses the dropdown OR chooses a color from this form?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24331739
Exactly...the color lists on the 2nd and 3rd tabs are probably ListBoxes.    =)

0
 
LVL 19

Author Comment

by:NerdsOfTech
ID: 24351472
What's the chances of coding this one out?

With an onclick event for the dropdown -> open a form underneth (could you make this a usercontrol; if you say had 2 dropdown color pickers and needed the same form / reuse / recycle?) -> select color...

I would love to see the code and a screenshot

:)

=NerdsOfTech

0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24369364
I'll see what I can whip up...though I'm really not sure what to do about that First Tab labeled "Custom"...

What to do with this tab?

CustomColorTab.jpg
0
 
LVL 19

Author Comment

by:NerdsOfTech
ID: 24369839
Use labels with sunken borders? Not sure either. Use your creativity!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24369911
No, no...  =)
(The control used doesn't matter...lots of ideas for that!)

What I mean is I have no idea where those colors are coming from.  Not sure if they come from some other dialog that I'm not aware of and they change whenever that one is changed.  I don't see anyway to modify those colors either...

I suppose I could grab the RGB Color Values using a screen scraper type tool and hard-code those colors in there?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24369942
Hmmm....the columns, with the exception of the first one, look like they might be a hard-coded selection of colors based on the ROYGBIV acrononym: Red, Orange, Yellow, Green, Blue, Indigo, Violet
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24370829
Here's a start...add the code below to a BLANK form:
(As a Dialog or UserControl, you could make it Raise a CustomEvent with the Selected Color)
Imports System.Reflection
Public Class Form1
 
    Private TC As New TabControl
    Private WithEvents WebListBox As New ListBox
    Private WithEvents SystemListBox As New ListBox
 
    Private WebColors As New List(Of KnownColor)
    Private SystemColors As New List(Of KnownColor)
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TC.TabPages.Add(New TabPage("Custom"))
        TC.TabPages.Add(New TabPage("Web"))
        TC.TabPages.Add(New TabPage("System"))
        TC.Dock = DockStyle.Fill
 
        BuildCustomColorTab()
        PopulateListBoxes()
 
        Me.Controls.Add(TC)
    End Sub
 
    Private Sub BuildCustomColorTab()
        Dim table As New TableLayoutPanel
        table.Dock = DockStyle.Fill
        table.GrowStyle = TableLayoutPanelGrowStyle.FixedSize
        table.RowCount = 6
        table.ColumnCount = 8
        table.RowStyles.Clear()
        For R As Integer = 1 To table.RowCount
            table.RowStyles.Add(New RowStyle(SizeType.Percent, 100 / table.RowCount))
        Next
        table.ColumnStyles.Clear()
        For C As Integer = 1 To table.ColumnCount
            table.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 100 / table.ColumnCount))
        Next
 
        Dim colors As New List(Of Color)
        ' Row 1
        colors.Add(Color.FromArgb(255, 255, 255))
        colors.Add(Color.FromArgb(255, 192, 192))
        colors.Add(Color.FromArgb(255, 224, 192))
        colors.Add(Color.FromArgb(255, 255, 192))
        colors.Add(Color.FromArgb(192, 255, 192))
        colors.Add(Color.FromArgb(192, 255, 255))
        colors.Add(Color.FromArgb(192, 192, 255))
        colors.Add(Color.FromArgb(255, 192, 255))
        ' Row 2
        colors.Add(Color.FromArgb(224, 224, 224))
        colors.Add(Color.FromArgb(255, 128, 128))
        colors.Add(Color.FromArgb(255, 192, 128))
        colors.Add(Color.FromArgb(255, 255, 128))
        colors.Add(Color.FromArgb(128, 255, 128))
        colors.Add(Color.FromArgb(128, 255, 255))
        colors.Add(Color.FromArgb(128, 128, 255))
        colors.Add(Color.FromArgb(255, 128, 255))
        ' Row 3
        colors.Add(Color.FromArgb(192, 192, 192))
        colors.Add(Color.FromArgb(255, 0, 0))
        colors.Add(Color.FromArgb(255, 128, 0))
        colors.Add(Color.FromArgb(255, 255, 0))
        colors.Add(Color.FromArgb(0, 255, 0))
        colors.Add(Color.FromArgb(0, 255, 255))
        colors.Add(Color.FromArgb(0, 0, 255))
        colors.Add(Color.FromArgb(255, 0, 255))
        ' Row 4
        colors.Add(Color.FromArgb(128, 128, 128))
        colors.Add(Color.FromArgb(192, 0, 0))
        colors.Add(Color.FromArgb(192, 64, 0))
        colors.Add(Color.FromArgb(192, 192, 0))
        colors.Add(Color.FromArgb(0, 192, 0))
        colors.Add(Color.FromArgb(0, 192, 192))
        colors.Add(Color.FromArgb(0, 0, 192))
        colors.Add(Color.FromArgb(192, 0, 192))
        ' Row 5
        colors.Add(Color.FromArgb(64, 64, 64))
        colors.Add(Color.FromArgb(128, 0, 0))
        colors.Add(Color.FromArgb(128, 64, 0))
        colors.Add(Color.FromArgb(128, 128, 0))
        colors.Add(Color.FromArgb(0, 128, 0))
        colors.Add(Color.FromArgb(0, 128, 128))
        colors.Add(Color.FromArgb(0, 0, 128))
        colors.Add(Color.FromArgb(128, 0, 128))
        ' Row 6
        colors.Add(Color.FromArgb(0, 0, 0))
        colors.Add(Color.FromArgb(64, 0, 0))
        colors.Add(Color.FromArgb(128, 64, 64))
        colors.Add(Color.FromArgb(64, 64, 0))
        colors.Add(Color.FromArgb(0, 64, 0))
        colors.Add(Color.FromArgb(0, 64, 64))
        colors.Add(Color.FromArgb(0, 0, 64))
        colors.Add(Color.FromArgb(64, 0, 64))
 
        For Each c As Color In colors
            Dim PB As New PictureBox
            PB.BorderStyle = BorderStyle.Fixed3D
            PB.Dock = DockStyle.Fill
            PB.BackColor = c
            AddHandler PB.Click, AddressOf pb_Click
            table.Controls.Add(PB)
        Next
        TC.TabPages(0).Controls.Add(table)
    End Sub
 
    Private Sub pb_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim pb As PictureBox = sender
        Me.Text = pb.BackColor.ToString
    End Sub
 
    Private Sub PopulateListBoxes()
        For Each C As Color In System.ComponentModel.TypeDescriptor.GetConverter(GetType(Color)).GetStandardValues
            If C.IsSystemColor Then
                SystemColors.Add(C.ToKnownColor)
            Else
                WebColors.Add(C.ToKnownColor)
            End If
        Next
        WebListBox.DataSource = WebColors
        WebListBox.DrawMode = DrawMode.OwnerDrawFixed
        WebListBox.Dock = DockStyle.Fill
        TC.TabPages(1).Controls.Add(WebListBox)
 
        SystemListBox.DataSource = SystemColors
        SystemListBox.DrawMode = DrawMode.OwnerDrawFixed
        SystemListBox.Dock = DockStyle.Fill
        TC.TabPages(2).Controls.Add(SystemListBox)
    End Sub
 
    Private Sub LB_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles WebListBox.DrawItem, SystemListBox.DrawItem
        Static width As Integer = 15
        Dim lb As ListBox = CType(sender, ListBox)
        Dim KC As KnownColor = lb.Items(e.Index)
        Dim rc As New Rectangle(e.Bounds.X + 3, e.Bounds.Y + 3, width, e.Bounds.Height - 6)
        e.DrawBackground()
        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, lb.Font, Brushes.Black, e.Bounds.X + (width + 6), e.Bounds.Y)
    End Sub
 
    Private Sub LB_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebListBox.SelectedValueChanged, SystemListBox.SelectedValueChanged
        Dim LB As ListBox = CType(sender, ListBox)
        Dim index As Integer = LB.SelectedIndex
        If index <> -1 Then
            Dim kc As KnownColor = LB.SelectedValue
            Dim C As Color = Color.FromKnownColor(kc)
            Me.Text = String.Format("Color [A={0}, R={1}, G={2}, B={3}]", C.A, C.R, C.G, C.B)
        End If
    End Sub
 
End Class

Open in new window

CustomColorDialog1.jpg
CustomColorDialog2.jpg
CustomColorDialog3.jpg
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
ID: 24375559
Here's a different implementation allowing you to use the keyboard to select a color from the ListBoxes.  You can use Enter or Double Click an Item.

The Main Form just has a Button on it which when clicked shows MyColorDialog below itself:
(the selected color becomes the Form BackColor)
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mcd As New MyColorDialog
        AddHandler mcd.ColorSelected, AddressOf mcd_ColorSelected
        Dim pt As Point = Button1.PointToScreen(New Point(0, 0))
        pt.Offset(0, Button1.Height)
        mcd.Location = pt
        mcd.ShowDialog(Me)
    End Sub
 
    Private Sub mcd_ColorSelected(ByVal clr As System.Drawing.Color)
        Me.BackColor = clr
    End Sub
 
End Class
 
Public Class MyColorDialog
    Inherits System.Windows.Forms.Form
 
    Private WithEvents TC As New TabControl
    Private WithEvents WebListBox As New ListBox
    Private WithEvents SystemListBox As New ListBox
    Private Table As New TableLayoutPanel
 
    Private WebColors As New List(Of KnownColor)
    Private SystemColors As New List(Of KnownColor)
 
    Public Event ColorSelected(ByVal clr As Color)
 
    Public Sub New()
        Me.StartPosition = FormStartPosition.Manual
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        Me.ShowInTaskbar = False
        Me.KeyPreview = True
 
        TC.TabPages.Add(New TabPage("Custom"))
        TC.TabPages.Add(New TabPage("Web"))
        TC.TabPages.Add(New TabPage("System"))
        TC.Dock = DockStyle.Fill
 
        BuildCustomColorTab()
        PopulateListBoxes()
 
        Me.Controls.Add(TC)
    End Sub
 
    Private Sub MyColorDialog_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Cursor.Clip = Me.RectangleToScreen(Me.ClientRectangle)
    End Sub
 
    Private Sub MyColorDialog_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Cursor.Clip = Nothing
    End Sub
 
    Private Sub MyColorDialog_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        ElseIf e.KeyCode = Keys.Enter Then
            Select Case TC.SelectedIndex
                Case 1, 2
                    Dim lb As ListBox = IIf(TC.SelectedIndex = 1, WebListBox, SystemListBox)
                    Dim index As Integer = lb.SelectedIndex
                    If index <> -1 Then
                        RaiseEvent ColorSelected(Color.FromKnownColor(lb.SelectedItem))
                        Me.Close()
                    End If
 
            End Select
        End If
    End Sub
 
    Private Sub BuildCustomColorTab()
        table.Dock = DockStyle.Fill
        table.GrowStyle = TableLayoutPanelGrowStyle.FixedSize
        table.RowCount = 6
        table.ColumnCount = 8
        table.RowStyles.Clear()
        For R As Integer = 1 To table.RowCount
            table.RowStyles.Add(New RowStyle(SizeType.Percent, 100 / table.RowCount))
        Next
        table.ColumnStyles.Clear()
        For C As Integer = 1 To table.ColumnCount
            table.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 100 / table.ColumnCount))
        Next
 
        Dim colors As New List(Of Color)
        ' Row 1
        colors.Add(Color.FromArgb(255, 255, 255))
        colors.Add(Color.FromArgb(255, 192, 192))
        colors.Add(Color.FromArgb(255, 224, 192))
        colors.Add(Color.FromArgb(255, 255, 192))
        colors.Add(Color.FromArgb(192, 255, 192))
        colors.Add(Color.FromArgb(192, 255, 255))
        colors.Add(Color.FromArgb(192, 192, 255))
        colors.Add(Color.FromArgb(255, 192, 255))
        ' Row 2
        colors.Add(Color.FromArgb(224, 224, 224))
        colors.Add(Color.FromArgb(255, 128, 128))
        colors.Add(Color.FromArgb(255, 192, 128))
        colors.Add(Color.FromArgb(255, 255, 128))
        colors.Add(Color.FromArgb(128, 255, 128))
        colors.Add(Color.FromArgb(128, 255, 255))
        colors.Add(Color.FromArgb(128, 128, 255))
        colors.Add(Color.FromArgb(255, 128, 255))
        ' Row 3
        colors.Add(Color.FromArgb(192, 192, 192))
        colors.Add(Color.FromArgb(255, 0, 0))
        colors.Add(Color.FromArgb(255, 128, 0))
        colors.Add(Color.FromArgb(255, 255, 0))
        colors.Add(Color.FromArgb(0, 255, 0))
        colors.Add(Color.FromArgb(0, 255, 255))
        colors.Add(Color.FromArgb(0, 0, 255))
        colors.Add(Color.FromArgb(255, 0, 255))
        ' Row 4
        colors.Add(Color.FromArgb(128, 128, 128))
        colors.Add(Color.FromArgb(192, 0, 0))
        colors.Add(Color.FromArgb(192, 64, 0))
        colors.Add(Color.FromArgb(192, 192, 0))
        colors.Add(Color.FromArgb(0, 192, 0))
        colors.Add(Color.FromArgb(0, 192, 192))
        colors.Add(Color.FromArgb(0, 0, 192))
        colors.Add(Color.FromArgb(192, 0, 192))
        ' Row 5
        colors.Add(Color.FromArgb(64, 64, 64))
        colors.Add(Color.FromArgb(128, 0, 0))
        colors.Add(Color.FromArgb(128, 64, 0))
        colors.Add(Color.FromArgb(128, 128, 0))
        colors.Add(Color.FromArgb(0, 128, 0))
        colors.Add(Color.FromArgb(0, 128, 128))
        colors.Add(Color.FromArgb(0, 0, 128))
        colors.Add(Color.FromArgb(128, 0, 128))
        ' Row 6
        colors.Add(Color.FromArgb(0, 0, 0))
        colors.Add(Color.FromArgb(64, 0, 0))
        colors.Add(Color.FromArgb(128, 64, 64))
        colors.Add(Color.FromArgb(64, 64, 0))
        colors.Add(Color.FromArgb(0, 64, 0))
        colors.Add(Color.FromArgb(0, 64, 64))
        colors.Add(Color.FromArgb(0, 0, 64))
        colors.Add(Color.FromArgb(64, 0, 64))
 
        For Each c As Color In colors
            Dim PB As New PictureBox
            PB.BorderStyle = BorderStyle.Fixed3D
            PB.Dock = DockStyle.Fill
            PB.BackColor = c
            AddHandler PB.Click, AddressOf pb_Click
            table.Controls.Add(PB)
        Next
        TC.TabPages(0).Controls.Add(table)
    End Sub
 
    Private Sub pb_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim pb As PictureBox = sender
        RaiseEvent ColorSelected(pb.BackColor)
        Me.Close()
    End Sub
 
    Private Sub PopulateListBoxes()
        For Each C As Color In System.ComponentModel.TypeDescriptor.GetConverter(GetType(Color)).GetStandardValues
            If C.IsSystemColor Then
                SystemColors.Add(C.ToKnownColor)
            Else
                WebColors.Add(C.ToKnownColor)
            End If
        Next
 
        WebListBox.DataSource = WebColors
        WebListBox.DrawMode = DrawMode.OwnerDrawFixed
        WebListBox.Dock = DockStyle.Fill
        TC.TabPages(1).Controls.Add(WebListBox)
 
        SystemListBox.DataSource = SystemColors
        SystemListBox.DrawMode = DrawMode.OwnerDrawFixed
        SystemListBox.Dock = DockStyle.Fill
        TC.TabPages(2).Controls.Add(SystemListBox)
    End Sub
 
    Private Sub LB_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles WebListBox.DrawItem, SystemListBox.DrawItem
        Static width As Integer = 15
        Dim lb As ListBox = CType(sender, ListBox)
        Dim KC As KnownColor = lb.Items(e.Index)
        Dim rc As New Rectangle(e.Bounds.X + 3, e.Bounds.Y + 3, width, e.Bounds.Height - 6)
        e.DrawBackground()
        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, lb.Font, Brushes.Black, e.Bounds.X + (width + 6), e.Bounds.Y)
    End Sub
 
    Private Sub LB_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles WebListBox.DoubleClick, SystemListBox.DoubleClick
        Dim LB As ListBox = CType(sender, ListBox)
        If LB.IsHandleCreated Then
            Dim pt As Point = LB.PointToClient(Cursor.Position)
            Dim index As Integer = LB.IndexFromPoint(pt)
            If index <> -1 Then
                Dim kc As KnownColor = LB.SelectedValue
                Dim C As Color = Color.FromKnownColor(kc)
                RaiseEvent ColorSelected(C)
                Me.Close()
                Debug.Print(String.Format("Color [A={0}, R={1}, G={2}, B={3}]", C.A, C.R, C.G, C.B))
            End If
        End If
    End Sub
 
    Private Sub TC_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TC.SelectedIndexChanged
        Select Case TC.SelectedIndex
            Case 0
                Table.Focus()
 
            Case 1
                WebListBox.Focus()
 
            Case 2
                SystemListBox.Focus()
 
        End Select
    End Sub
 
End Class

Open in new window

0
 
LVL 19

Author Closing Comment

by:NerdsOfTech
ID: 31579264
Great intelligence and remarkable abilities in Visual Basic 2008! You show an exceptional natural capacity in the production of concepts into working, living applications. You are gifted and I thank you again for your assistance. Genius!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question