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

Re: Combobox question by Bart

Bart
Sat Mar 15 10:49:49 CDT 2008

Fixed it already

bart



Re: Combobox question by Peter

Peter
Sat Mar 15 13:12:21 CDT 2008

On Sat, 15 Mar 2008 08:49:49 -0700, Bart wrote:

> Fixed it already

If you post a question and then solve it yourself, it is polite and
customary to post your solution.

Otherwise, the implication you're making is that this newsgroup is here
only to serve you, rather than to help others. Surely that's not the
implication you want to make?

Pete

Re: Combobox question by Bart

Bart
Sun Mar 16 03:19:06 CDT 2008

>
> If you post a question and then solve it yourself, it is polite and
> customary to post your solution.
>
> Otherwise, the implication you're making is that this newsgroup is here
> only to serve you, rather than to help others. Surely that's not the
> implication you want to make?
>
> Pete

I am trully sorry for that peter and of course you are right. I apologize
for my rudeness.

namespace SettingsTest.Controls
{
class ColorComboBox : ComboBox
{
public ColorComboBox(): base()
{
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.DrawMode = DrawMode.OwnerDrawFixed;
this.PopulateWithColors();
this.DrawItem += new
DrawItemEventHandler(ColorComboBox_DrawItem);
}

private void PopulateWithColors()
{
this.Items.Clear();

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);
this.Items.Add(color.Name);
}
}
}

void ColorComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
string ColorName = string.Empty;

if (e.Index >= 0)
{
ColorName = (string)((ComboBox)sender).Items[e.Index];
}

// Get the Bounding rectangle for a selected item
Rectangle SelRect = new Rectangle(e.Bounds.X, 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(5, e.Bounds.Top + 2, 11,
e.Bounds.Height - 4);

using (Brush TextBrush = new SolidBrush(e.BackColor))
{
using (Brush RectBrush = new
SolidBrush(Color.FromName(ColorName)))
{
// Paint the item
e.Graphics.FillRectangle(TextBrush, SelRect);
// Paint the litle rectangle in front of the colorname
e.Graphics.FillRectangle(RectBrush, ColorRect);
e.DrawFocusRectangle();
}
}
using (Brush brush = new SolidBrush(e.ForeColor))
{
e.Graphics.DrawString(ColorName, e.Font, brush, 20,
e.Bounds.Top);
}

}
}
}

Thank you for giving me the right direction :)

Regards,

Bart