How to set different colors for items of NListBox


https://www.nevron.com/Forum/Topic6605.aspx
Print Topic | Close Window

By Partho Sarathi - 12 Years Ago
How do I set different color background/foreground for items? From what I can understand, this will require writing a custom renderer. I tried to extend NListBoxRenderer but a lot of it's members seem to be Internal. All I need is to be able to change the foreground and background colors for individual items. Can this be done easily?
By Nevron Support - 12 Years Ago
Hi Partho,

Yes you need to extend NListBoxRenderer.

To change the back color you need to override DrawItemBackground method, and to change the fore color you just need to change Palette.WindowText and Palette.HighlightText.

Here is an example implementation of the DrawItemBackground method:


public override void DrawItemBackground(Graphics g, NListBoxItem item)
{
if (m_List.CurrentSkin != null)
base.DrawItemBackground(g, item);

if (!m_List.Enabled)
return;



if (item.ListBox.SelectedItem == item)
{
base.DrawItemBackground(g, item);
}
else
{
if (m_List.ColumnOnLeft)
{
DrawColumn(g, item);
}

//Here you can use an item.Tag property to provide the desired color instead of the index.
if (item.Index == 1)
{
m_Brush.Color = Color.Red;
}
else
{
m_Brush.Color = m_Palette.Window;
}
g.FillRectangle(m_Brush, m_TextRect);
}
}