471,825 Members | 957 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,825 software developers and data experts.

Selectively change the color of an Item in a listbox

Is it possible to selectivly change the color of an item in text. I
saw ForeColor option, but it changes the color of all the items. If it
is not possible, is there any other control list listbox where we can
see more than one item and change the color during run time.
Thanks.
Apr 4 '08 #1
3 29145
On Apr 4, 5:47*pm, CSharper <cshar...@gmx.comwrote:
Is it possible to selectivly change the color of an item in text. I
saw ForeColor option, but it changes the color of all the items. If it
is not possible, is there any other control list listbox where we can
see more than one item and change the color during run time.
Thanks.
IIRC this is not allowed.

You would have to implement it your self.

Do a search by owned draw control in opennetcf.org to see an example.
Even as it's for the CF the same idea applies
Apr 4 '08 #2

"CSharper" <cs******@gmx.comschreef in bericht
news:41**********************************@a22g2000 hsc.googlegroups.com...
Is it possible to selectivly change the color of an item in text. I
saw ForeColor option, but it changes the color of all the items. If it
is not possible, is there any other control list listbox where we can
see more than one item and change the color during run time.
Thanks.


i think this should do,

private void PopulateListBoxes()
{

Updating = true;

areaListBox.Items.Add(new AreaStyle("Keywords", Color.Black,
Color.White, "Standard"));

areaListBox.Items.Add(new AreaStyle("Comments", Color.Green,
Color.White, "Standard"));

areaListBox.Items.Add(new AreaStyle("Numbers", Color.Navy, Color.Yellow,
"Bold"));

areaListBox.Items.Add(new AreaStyle("Strings", Color.Maroon,
Color.White, "Standard"));

areaListBox.Items.Add(new AreaStyle("Local variables", Color.OrangeRed,
Color.White, "Bold"));

areaListBox.Items.Add(new AreaStyle("Boolean operators", Color.Black,
Color.White, "Bold"));

areaListBox.Items.Add(new AreaStyle("Signals", Color.CadetBlue,
Color.White, "Bold"));

}

private void areaListBox_DrawItem(object sender, DrawItemEventArgs e)
{

ListBox list = (ListBox)sender;

AreaStyle style = (AreaStyle)list.Items[e.Index];
Color foreColor = Color.Empty;

Color backColor = Color.Empty;

// Get the Bounding rectangle for a selected item

Rectangle SelRect = new Rectangle(e.Bounds.X, e.Bounds.Y,
e.Bounds.Width - 1, e.Bounds.Height - 1);

using(Brush backBrush = new SolidBrush(style.BackColor))

{

// Paint the item background in the wanted color

e.Graphics.FillRectangle(backBrush, SelRect);

}
using (Pen p = new Pen(Color.Empty))

{

if(e.State == DrawItemState.Selected)

{

// Set the pen color

p.Color = Color.Black;

}

else

{

// Set the pen color

p.Color = list.BackColor;

}

// Draw the selection rectangle in either black or the lisbox
backcolor to hide it

e.Graphics.DrawRectangle(p, SelRect);

}

e.DrawFocusRectangle();

using(Brush brush = new SolidBrush(style.ForeColor))

{

using(Font font = new Font(list.Font,
GetFonstyleFromName(style.FontStyle)))

{

string text = list.Items[e.Index].ToString();

e.Graphics.DrawString(text, font, brush, e.Bounds.X, e.Bounds.Y
+ 1);

}

}

}

private void areaListBox_MeasureItem(object sender, MeasureItemEventArgs e)

{

// Increase slightly to create some room for the selection rectangle

e.ItemHeight += 2;

}

bart
Apr 4 '08 #3
"CSharper" <cs******@gmx.comwrote in message
news:41**********************************@a22g2000 hsc.googlegroups.com...
Is it possible to selectivly change the color of an item in text. I
saw ForeColor option, but it changes the color of all the items. If it
is not possible, is there any other control list listbox where we can
see more than one item and change the color during run time.
Thanks.
One option is to use a ListView instead. It allows the color to be set
indvidually for each item.
/claes
Apr 7 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mamatha | last post: by
1 post views Thread by Vd | last post: by
6 posts views Thread by sr | last post: by
3 posts views Thread by nma | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.