Advertisement
Jimi2000

ComboBoxColorSelector

Sep 21st, 2022 (edited)
1,452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Text;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8.  
  9.     [DesignerCategory("Code")]
  10.     [ToolboxItem(true), ToolboxBitmap(typeof(ComboBox))] // Generic: replace with custom Icon
  11.     public class ComboBoxColorSelector : ComboBox
  12.     {
  13.         private Color defaultColor = Color.Transparent;
  14.         public ComboBoxColorSelector() => InitializeComponent();
  15.  
  16.         private void InitializeComponent()
  17.         {
  18.             DropDownStyle = ComboBoxStyle.DropDownList;
  19.             DrawMode = DrawMode.OwnerDrawVariable;
  20.             FlatStyle = FlatStyle.Flat;
  21.             FormattingEnabled = false;
  22.         }
  23.        
  24.         // Read this Property value when a selection is made
  25.         // This is the Color currently selected
  26.         public Color SelectedColor { get; private set; }
  27.  
  28.         protected override void OnHandleCreated(EventArgs e)
  29.         {
  30.             base.OnHandleCreated(e);
  31.             if (DesignMode || Items.Count > 0) return;
  32.             Color[] colors =
  33.                 Enum.GetValues(typeof(KnownColor)).OfType<KnownColor>()
  34.                     .Select(kc => Color.FromKnownColor(kc)).ToArray();
  35.             DisplayMember = "Name";
  36.             DataSource = colors;
  37.         }
  38.  
  39.         int previousFound = 0;
  40.         protected override void OnKeyPress(KeyPressEventArgs e)
  41.         {
  42.             base.OnKeyPress(e);
  43.             if (char.IsLetter(e.KeyChar)) {
  44.                 int found = FindString(e.KeyChar.ToString().ToUpper(), previousFound);
  45.                 if (found > -1) {
  46.                     previousFound = found;
  47.                     SelectedIndex = found;
  48.                 }
  49.                 else {
  50.                     previousFound = 0;
  51.                 }
  52.             }
  53.         }
  54.  
  55.         protected override void OnDrawItem(DrawItemEventArgs e)
  56.         {
  57.             e.DrawBackground();
  58.             if (e.Index < 0) return;
  59.  
  60.             e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  61.             var itemColor = (Color)Items[e.Index];
  62.             using (Brush itemBrush = new SolidBrush(itemColor))
  63.             using (Brush textBrush = new SolidBrush(e.ForeColor)) {
  64.                 e.Graphics.FillRectangle(itemBrush, new Rectangle(new Point(e.Bounds.X + 1, e.Bounds.Y + 1), new Size(e.Bounds.Height - 1, e.Bounds.Height - 1)));
  65.                 e.Graphics.DrawString(itemColor.Name, e.Font, textBrush, new Point(e.Bounds.X + e.Bounds.Height + 10, e.Bounds.Y));
  66.             }
  67.             e.DrawFocusRectangle();
  68.             base.OnDrawItem(e);
  69.         }
  70.  
  71.         protected override void OnMeasureItem(MeasureItemEventArgs e)
  72.         {
  73.             e.ItemHeight = Font.Height + 4;
  74.             base.OnMeasureItem(e);
  75.         }
  76.  
  77.         protected override void OnSelectionChangeCommitted(EventArgs e)
  78.         {
  79.             if (SelectedIndex >= 0) SelectedColor = (Color)SelectedItem;
  80.             base.OnSelectionChangeCommitted(e);
  81.         }
  82.  
  83.         protected override void OnSelectedIndexChanged(EventArgs e)
  84.         {
  85.             if (SelectedIndex >= 0) SelectedColor = (Color)SelectedItem;
  86.             base.OnSelectedIndexChanged(e);
  87.         }
  88.  
  89.         protected override void OnParentForeColorChanged(EventArgs e)
  90.         {
  91.             base.OnParentForeColorChanged(e);
  92.             if (this.Parent != null) defaultColor = this.Parent.ForeColor;
  93.         }
  94.     }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement