Reputation: 157
i have to List box one have Listbox have circle and its value i m dynamically create Circle as Per size come from database ..... i want to fill color in List box where circle is draw from another List box where Items have color like Red,blue, in one item selected in circle list box and then click on second List box color then automatically fill circle list box circle color .. Maximum color is selected in color list box is 2 .. Means if there are two color selected then half is first color and half is second color fill.. this is my requirement can you any buddy help me regurding this how to fill circle color...
private void Form1_Load(object sender, EventArgs e)
{
listBox1.DrawMode = DrawMode.OwnerDrawVariable;
listBox1.Items.Add("One");
listBox1.Items.Add("Two");
listBox1.Items.Add("Three");
listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
}
void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox l=sender as ListBox;
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawEllipse(Pens.Blue, new Rectangle(1, 1+e.Index * 15, 100, 10));
e.Graphics.DrawString(l.Items[e.Index].ToString(),
new Font(FontFamily.GenericSansSerif,9, FontStyle.Regular),
Brushes.Red , e.Bounds);
}
Upvotes: 0
Views: 1887
Reputation: 1663
Not sure I understand what you mean, but if you want to draw a circle will a fill colour, then do:
e.Graphics.FillEllipse(Brushes.Blue, new Rectangle(1, 1 + e.Index * 15, 10, 10));
instead of DrawEllipse
.
Upvotes: 0
Reputation: 273854
It seems that part of your question is how to fill an ellipse, that looks like:
using (Brush fill = new Brush( ...))
{
e.Graphics.FillEllipse(fill, new Rectangle(1, 1+e.Index * 15, 100, 10));
}
I'm not sure how you want to use that second listbox, is it a list of Color-names?
I gave it a little try:
in Form_Load,
listBox2.Items.Add(Color.Blue);
listBox2.Items.Add(Color.Green);
listBox2.Items.Add(Color.Red);
in listBox1_DrawItem
Color back = Color.Black;
if (listBox2.SelectedIndex >= 0)
back = (Color)listBox2.SelectedItem;
using (Brush fill = new SolidBrush(back))
using (Font text = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular))
{
e.Graphics.FillEllipse(fill, new Rectangle(1, 1+e.Index * 15, 100, 10));
e.Graphics.DrawEllipse(Pens.Blue, new Rectangle(1, 1 + e.Index * 15, 100, 10));
e.Graphics.DrawString(l.Items[e.Index].ToString(), text, Brushes.Red, e.Bounds);
}
Note also the using pattern, you really should make that a habit.
And finally, in listBox2_SelectedIndexChanged
listBox1.Invalidate();
Upvotes: 1