Color Picker Combobox in C#
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Drawing.Drawing2D ;
using
System.Linq;
using
System.IO;
using
System.Text;
using
System.Windows.Forms;
using
System.Reflection;  
namespace
WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public
Form1()
        {
            InitializeComponent();
        }
        private
void Form1_Load(object
sender, EventArgs e)
        {
            Type
t = typeof(Color);
            PropertyInfo[]
p = t.GetProperties();
            foreach
(PropertyInfo item in
p)
            {
                if
(item.PropertyType.FullName.Equals("System.Drawing.Color",
StringComparison.CurrentCultureIgnoreCase))
                {
                    this.comboBox1.Items.Add(item.Name);
                }
            }
        }
        private
void comboBox1_DrawItem(object sender, DrawItemEventArgs
e)
        {
            if
(e.Index != -1)
            {
                e.DrawBackground();
               
e.Graphics.FillRectangle(GetCurrentBrush(comboBox1.Items[e.Index].ToString()),
e.Bounds);
                Font
f = comboBox1.Font;
                e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(),
f, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
                e.DrawFocusRectangle();
            }
        }
        private
Brush GetCurrentBrush(string colorName)
        {
            return
new SolidBrush(Color.FromName(colorName));
        }
    }
}

No comments:
Post a Comment