B
Bart
Hi all,
I have a need to have a combo box filled with available colors. The user
then can pick the color he desires. I have acomplished to fill a combobox
with the colors and a little rectangle in front of all the color names to
represent the actual color. But wat I like to have is that after a user
selected the color the selection and also the rectangle would be shown.
Any one tha can help me out here.
Here is my code sofar:
public partial class ColorsControl : BaseControl
{
public ColorsControl() : base()
{
InitializeComponent();
PopulateListBoxes();
}
private void PopulateListBoxes()
{
Color colorTable = new Color();
Type t = typeof(Color);
System.Reflection.PropertyInfo[] PropInfos = t.GetProperties();
foreach (System.Reflection.PropertyInfo PropInfo in PropInfos)
{
if (PropInfo.PropertyType == typeof(Color))
{
Color color = (Color)PropInfo.GetValue(colorTable,
null);
comboBox1.Items.Add(color.Name);
}
}
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
string color = (string)((ComboBox)sender).Items[e.Index];
// Get the Bounding rectangle for a selected item
Rectangle SelRect = new Rectangle(e.Bounds.X + 15, e.Bounds.Y,
e.Bounds.Width, e.Bounds.Height);
// Get the Bounding rectangle for the color rectangle in front
of the items
Rectangle ColorRect = new Rectangle(2, e.Bounds.Top + 1, 10,
e.Bounds.Height - 4);
using (Brush TextBrush = new SolidBrush(e.BackColor))
{
using (Brush RectBrush = new
SolidBrush(Color.FromName(color)))
{
// Paint the item accordingly if it is selected
e.DrawFocusRectangle();
e.Graphics.FillRectangle(TextBrush, SelRect);
// Paint the litle rectangle in front of the colorname
e.Graphics.FillRectangle(RectBrush, ColorRect);
}
}
using (Brush brush = new SolidBrush(e.ForeColor))
{
e.Graphics.DrawString(color, e.Font, brush, 15,
e.Bounds.Top);
}
}
}
Thanks a lot in advance, Regards,
Bart
I have a need to have a combo box filled with available colors. The user
then can pick the color he desires. I have acomplished to fill a combobox
with the colors and a little rectangle in front of all the color names to
represent the actual color. But wat I like to have is that after a user
selected the color the selection and also the rectangle would be shown.
Any one tha can help me out here.
Here is my code sofar:
public partial class ColorsControl : BaseControl
{
public ColorsControl() : base()
{
InitializeComponent();
PopulateListBoxes();
}
private void PopulateListBoxes()
{
Color colorTable = new Color();
Type t = typeof(Color);
System.Reflection.PropertyInfo[] PropInfos = t.GetProperties();
foreach (System.Reflection.PropertyInfo PropInfo in PropInfos)
{
if (PropInfo.PropertyType == typeof(Color))
{
Color color = (Color)PropInfo.GetValue(colorTable,
null);
comboBox1.Items.Add(color.Name);
}
}
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
string color = (string)((ComboBox)sender).Items[e.Index];
// Get the Bounding rectangle for a selected item
Rectangle SelRect = new Rectangle(e.Bounds.X + 15, e.Bounds.Y,
e.Bounds.Width, e.Bounds.Height);
// Get the Bounding rectangle for the color rectangle in front
of the items
Rectangle ColorRect = new Rectangle(2, e.Bounds.Top + 1, 10,
e.Bounds.Height - 4);
using (Brush TextBrush = new SolidBrush(e.BackColor))
{
using (Brush RectBrush = new
SolidBrush(Color.FromName(color)))
{
// Paint the item accordingly if it is selected
e.DrawFocusRectangle();
e.Graphics.FillRectangle(TextBrush, SelRect);
// Paint the litle rectangle in front of the colorname
e.Graphics.FillRectangle(RectBrush, ColorRect);
}
}
using (Brush brush = new SolidBrush(e.ForeColor))
{
e.Graphics.DrawString(color, e.Font, brush, 15,
e.Bounds.Top);
}
}
}
Thanks a lot in advance, Regards,
Bart