Profile Picture

How to set different colors for items of NListBox

Posted By Partho Sarathi 12 Years Ago
Author
Message
Partho Sarathi
Posted 12 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: Forum Members
Last Active: 12 Years Ago
Posts: 9, Visits: 1
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?

Nevron Support
Posted 12 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)

Group: Forum Members
Last Active: Last Year
Posts: 3,039, Visits: 3,746
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);
}
}


Best Regards,
Nevron Support Team





Similar Topics


Reading This Topic