Advertisement
JayBeeOH

Colors ComboBox Demo

Oct 8th, 2016
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.89 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Colors ComboBox Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   24 SEP 2016
  16. '
  17. ' Description:    Show installed colors in combobox.
  18. '
  19. '                 Documentation is at:
  20. '                   App's screen image is at: http://imgur.com/7CYiZIc
  21. '                   App's Visual Basic .NET code is at http://pastebin.com/1j94mv1J
  22. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  23. '-------------------------------------------------------------------------------------------
  24.  
  25. Public Class MainForm
  26.  
  27.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  28.         Handles MyBase.Load
  29.  
  30.         ' Get the system colors and load the combo box using LINQ excluding
  31.         ' SystemColors and the Transparent color.
  32.         Dim knowColors = System.Enum.GetNames(GetType(KnownColor)).
  33.             Where(Function(kc) GetType(SystemColors).GetProperty(kc) Is Nothing _
  34.                 AndAlso kc <> KnownColor.Transparent.ToString()).
  35.             OrderBy(Function(kc) kc)
  36.  
  37.         With ColorsComboBox
  38.             ' Allow user to draw control.
  39.             .DrawMode = DrawMode.OwnerDrawFixed
  40.             ' If you wish to control MaxDropDownItems, set IntegralHeight property to false.
  41.             .IntegralHeight = False
  42.             .MaxDropDownItems = 8
  43.             .DataSource = knowColors.ToList
  44.             .SelectedIndex = -1
  45.         End With
  46.         AddHandler ColorsComboBox.SelectedIndexChanged, AddressOf ColorsComboBox_SelectedIndexChanged
  47.     End Sub
  48.  
  49.     ' Change the font of label when FontsComboBox changes value.
  50.     Private Sub ColorsComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
  51.  
  52.         If ColorsComboBox.SelectedIndex <> -1 Then
  53.             ColorsExampleLabel.Text = ColorsComboBox.SelectedItem.ToString
  54.             ColorsExampleLabel.BackColor = Color.FromName(ColorsComboBox.SelectedItem.ToString)
  55.             'ColorsComboBox.SelectedIndex = -1
  56.         End If
  57.     End Sub
  58.  
  59.     ' Draw each item in the control's collection with the item's font.
  60.     Private Sub ColorsComboBox_DrawItem(sender As Object, e As DrawItemEventArgs) _
  61.         Handles ColorsComboBox.DrawItem
  62.  
  63.         Dim myComboBox As ComboBox = CType(sender, ComboBox)
  64.         Dim mySelectedColor As Color = Color.FromName(myComboBox.Items(e.Index).ToString)
  65.         Dim myRectangleSize As Integer = e.Bounds.Height - 4
  66.  
  67.         ' Draw the background of the ComboBox control for each item.
  68.         e.DrawBackground()
  69.  
  70.         ' Declare the default color of the text brush and the fill brush.
  71.         Using mybrush As New SolidBrush(e.ForeColor)                    ' For the text color.
  72.             Using mySelectedBrush As New SolidBrush(mySelectedColor)    ' For the fill color.
  73.  
  74.                 'Drawing a rectangle (in this example, small square) for the color values
  75.  
  76.                 'Draw the colored rectangle...
  77.                 e.Graphics.FillRectangle(mySelectedBrush,
  78.                                          e.Bounds.Left + 5,
  79.                                          e.Bounds.Top + 2,
  80.                                          myRectangleSize,
  81.                                          myRectangleSize)
  82.                 'Overlay and draw boarder...
  83.                 e.Graphics.DrawRectangle(New Pen(Brushes.Black),
  84.                                          e.Bounds.Left + 5,
  85.                                          e.Bounds.Top + 2,
  86.                                          myRectangleSize,
  87.                                          myRectangleSize)
  88.  
  89.                 ' Draw the current item's text based on the current font and brush settings.
  90.                 e.Graphics.DrawString(mySelectedColor.Name,
  91.                                       myComboBox.Font,
  92.                                       mybrush,
  93.                                       e.Bounds.Left + myRectangleSize + 10,
  94.                                       e.Bounds.Top + 2,
  95.                                       StringFormat.GenericDefault)
  96.  
  97.             End Using
  98.         End Using
  99.  
  100.         ' If the control has focus, draw a focus rectangle around the selected item.
  101.         e.DrawFocusRectangle()
  102.     End Sub
  103.  
  104. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement