Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ListBox Colours

ListBox Colours

Scheduled Pinned Locked Moved C#
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex Grose
    wrote on last edited by
    #1

    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.

    X D 2 Replies Last reply
    0
    • A Alex Grose

      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.

      X Offline
      X Offline
      Xmen Real
      wrote on last edited by
      #2

      Grab this mate ;) http://www.codeproject.com/KB/webforms/ColorListBox.aspx[^]

      1 Reply Last reply
      0
      • A Alex Grose

        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.

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        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)

        A 1 Reply Last reply
        0
        • D DaveyM69

          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)

          A Offline
          A Offline
          Alex Grose
          wrote on last edited by
          #4

          This is sort of what I want, but to be able to change the colour dynamically within the script (but not fixed at start)

          D 1 Reply Last reply
          0
          • A Alex Grose

            This is sort of what I want, but to be able to change the colour dynamically within the script (but not fixed at start)

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            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)

            A 1 Reply Last reply
            0
            • D DaveyM69

              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)

              A Offline
              A Offline
              Alex Grose
              wrote on last edited by
              #6

              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.

              D 1 Reply Last reply
              0
              • A Alex Grose

                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.

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups