469,354 Members | 2,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,354 developers. It's quick & easy.

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 28994
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.