ListBox Colours
-
Is there any way to change the background colour of individual items in a listBox through code? I know you can do it with the listView control, but I have already built most of the functionality for the listBox, and don't really want to have to redo it.
-
Is there any way to change the background colour of individual items in a listBox through code? I know you can do it with the listView control, but I have already built most of the functionality for the listBox, and don't really want to have to redo it.
-
Is there any way to change the background colour of individual items in a listBox through code? I know you can do it with the listView control, but I have already built most of the functionality for the listBox, and don't really want to have to redo it.
Set the listBox's DrawMode property to OwnerDrawFixed. Subscribe to the listBox's DrawItem event. In the event handler method put this:
// will draw every other item with Red background
if (e.Index % 2 != 0)
{
e = new DrawItemEventArgs(
e.Graphics,
e.Font,
e.Bounds,
e.Index,
e.State,
e.ForeColor, Color.Red);
}
e.DrawBackground();
if (sender is ListBox)
{
ListBox listBox = (ListBox)sender;
e.Graphics.DrawString(
listBox.Items[e.Index].ToString(),
e.Font,
new SolidBrush(e.ForeColor),
e.Bounds);
}The reason for the new DrawItemEventArgs is that the e.BackColor property is readonly.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Set the listBox's DrawMode property to OwnerDrawFixed. Subscribe to the listBox's DrawItem event. In the event handler method put this:
// will draw every other item with Red background
if (e.Index % 2 != 0)
{
e = new DrawItemEventArgs(
e.Graphics,
e.Font,
e.Bounds,
e.Index,
e.State,
e.ForeColor, Color.Red);
}
e.DrawBackground();
if (sender is ListBox)
{
ListBox listBox = (ListBox)sender;
e.Graphics.DrawString(
listBox.Items[e.Index].ToString(),
e.Font,
new SolidBrush(e.ForeColor),
e.Bounds);
}The reason for the new DrawItemEventArgs is that the e.BackColor property is readonly.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)This is sort of what I want, but to be able to change the colour dynamically within the script (but not fixed at start)
-
This is sort of what I want, but to be able to change the colour dynamically within the script (but not fixed at start)
Just alter the condition in the if block - or change to a switch... or whatever you want
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) -
Just alter the condition in the if block - or change to a switch... or whatever you want
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)Still not quite. Perhaps I havn't explained properly. I am basically creating an RSS reader. If the post (stored in a static Global list) hasn't been read before, it has a value called 'isNew' set to true - this should mean it will have a background colour other than white. When the user clicks this item, this value will change to false and I want the backing colour (marking it as unread) to change back to white. Basically, I want to change the colour at various points during the code (not just at initial Draw time). Thanks.
-
Still not quite. Perhaps I havn't explained properly. I am basically creating an RSS reader. If the post (stored in a static Global list) hasn't been read before, it has a value called 'isNew' set to true - this should mean it will have a background colour other than white. When the user clicks this item, this value will change to false and I want the backing colour (marking it as unread) to change back to white. Basically, I want to change the colour at various points during the code (not just at initial Draw time). Thanks.
You should be able to make it work from what I gave you before. I've expanded it a little to help. This works for me given your scenario - you should be able to adapt it to suit your existing code. A basic Feed class:
public class Feed
{
public Feed(string displayText)
{
m_DisplayText = displayText;
isNew = true;
}
private string m_DisplayText;
private bool isNew;
public string DisplayText
{
get { return m_DisplayText; }
}
public bool IsNew
{
get { return isNew; }
}
public void SetRead()
{
isNew = false;
}
public override string ToString()
{
return m_DisplayText;
}
}This inside the class that holds the ListBox (lstFeeds) and call FillList() after any default initialization.
private Color unreadForeColor = SystemColors.Window;
private Color unreadBackColor = SystemColors.ControlText;
[DefaultValue(typeof(Color), "Window")]
public Color UnreadForeColor
{
get { return unreadForeColor; }
set { unreadForeColor = value; }
}
[DefaultValue(typeof(Color), "ControlText")]
public Color UnreadBackColor
{
get { return unreadBackColor; }
set { unreadBackColor = value; }
}
private void FillList()
{
lstFeeds.DrawMode = DrawMode.OwnerDrawFixed;
lstFeeds.SelectedIndexChanged += new EventHandler(lstFeeds_SelectedIndexChanged);
lstFeeds.DrawItem += new DrawItemEventHandler(lstFeeds_DrawItem);
Feed[] feeds = new Feed[] {
new Feed("Feed A"),
new Feed("Feed B"),
new Feed("Feed C"),
new Feed("Feed D")};
lstFeeds.Items.AddRange(feeds);
}
void lstFeeds_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstFeeds.SelectedIndices.Count > 0)
{
foreach (int selectedIndex in lstFeeds.SelectedIndices)
{
Feed selectedFeed = (Feed)lstFeeds.Items[selectedIndex];
selectedFeed.SetRead();
}
}
}
void lstFeeds_DrawItem(object sender, DrawItemEventArgs e)
{
Feed thisItem = (Feed)lstFeeds.Items[e.Index];
if (thisItem.IsNew)
{
e = new DrawItemEventArgs(
e.Graphics,
e.Font,
e.Bounds,
e.Index,
e.State,
UnreadForeColor,
UnreadBackColor);
}
e.DrawBackground();
if (sender is ListBox)
{
ListBox listBox = (ListBox)sender;
e.Graphics.DrawString(
listBox.Items[e.Index].ToS