Selectively change the color of an Item in a listbox

  • Thread starter Thread starter CSharper
  • Start date Start date
C

CSharper

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.
 
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
 
CSharper said:
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
 
CSharper said:
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
 
Back
Top