Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: Colors ComboBox Demo
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 24 SEP 2016
- '
- ' Description: Show installed colors in combobox.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/7CYiZIc
- ' App's Visual Basic .NET code is at http://pastebin.com/1j94mv1J
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Public Class MainForm
- Private Sub MainForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- ' Get the system colors and load the combo box using LINQ excluding
- ' SystemColors and the Transparent color.
- Dim knowColors = System.Enum.GetNames(GetType(KnownColor)).
- Where(Function(kc) GetType(SystemColors).GetProperty(kc) Is Nothing _
- AndAlso kc <> KnownColor.Transparent.ToString()).
- OrderBy(Function(kc) kc)
- With ColorsComboBox
- ' Allow user to draw control.
- .DrawMode = DrawMode.OwnerDrawFixed
- ' If you wish to control MaxDropDownItems, set IntegralHeight property to false.
- .IntegralHeight = False
- .MaxDropDownItems = 8
- .DataSource = knowColors.ToList
- .SelectedIndex = -1
- End With
- AddHandler ColorsComboBox.SelectedIndexChanged, AddressOf ColorsComboBox_SelectedIndexChanged
- End Sub
- ' Change the font of label when FontsComboBox changes value.
- Private Sub ColorsComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
- If ColorsComboBox.SelectedIndex <> -1 Then
- ColorsExampleLabel.Text = ColorsComboBox.SelectedItem.ToString
- ColorsExampleLabel.BackColor = Color.FromName(ColorsComboBox.SelectedItem.ToString)
- 'ColorsComboBox.SelectedIndex = -1
- End If
- End Sub
- ' Draw each item in the control's collection with the item's font.
- Private Sub ColorsComboBox_DrawItem(sender As Object, e As DrawItemEventArgs) _
- Handles ColorsComboBox.DrawItem
- Dim myComboBox As ComboBox = CType(sender, ComboBox)
- Dim mySelectedColor As Color = Color.FromName(myComboBox.Items(e.Index).ToString)
- Dim myRectangleSize As Integer = e.Bounds.Height - 4
- ' Draw the background of the ComboBox control for each item.
- e.DrawBackground()
- ' Declare the default color of the text brush and the fill brush.
- Using mybrush As New SolidBrush(e.ForeColor) ' For the text color.
- Using mySelectedBrush As New SolidBrush(mySelectedColor) ' For the fill color.
- 'Drawing a rectangle (in this example, small square) for the color values
- 'Draw the colored rectangle...
- e.Graphics.FillRectangle(mySelectedBrush,
- e.Bounds.Left + 5,
- e.Bounds.Top + 2,
- myRectangleSize,
- myRectangleSize)
- 'Overlay and draw boarder...
- e.Graphics.DrawRectangle(New Pen(Brushes.Black),
- e.Bounds.Left + 5,
- e.Bounds.Top + 2,
- myRectangleSize,
- myRectangleSize)
- ' Draw the current item's text based on the current font and brush settings.
- e.Graphics.DrawString(mySelectedColor.Name,
- myComboBox.Font,
- mybrush,
- e.Bounds.Left + myRectangleSize + 10,
- e.Bounds.Top + 2,
- StringFormat.GenericDefault)
- End Using
- End Using
- ' If the control has focus, draw a focus rectangle around the selected item.
- e.DrawFocusRectangle()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement