Setting the foreground color of a row based on pre-defined conditions.

This is a discussion on Setting the foreground color of a row based on pre-defined conditions. within the C# Programming forums, part of the General Programming Boards category; I have done the following using the advice/example from the web:- 1) Added a listbox (listBox1) to a form and ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Setting the foreground color of a row based on pre-defined conditions.

    I have done the following using the advice/example from the web:-

    1) Added a listbox (listBox1) to a form and set the DrawMode = OwnerDrawFixed.

    2) Added the following to the immediately following InitializeComponent();
    listBox1.DrawItem +=
    new System.Windows.Forms.DrawItemEventHandler(this.lis tBox1_DrawItem);

    3) In Form1_Load, I have added the following to the listBox1 -
    listBox1.Items.Add("ABC Company");
    listBox1.Items.Add("DEF Company");
    listBox1.Items.Add("GHI Company");
    listBox1.Items.Add("JKL Comapny");
    listBox1.Items.Add(MNO Company");

    4) I have also build up an arrayList while adding items to listBox1 so that the items correspond to the listBox1 item. The arrayList contains the following in formation -
    arrayList[0] = "Normal";
    arrayList[1] = "Active";
    arrayList[2] = "Active";
    arrayList[3] = "Other";
    arrayList[4] = "Normal";

    5) Now I want the items appearing in listBox1 to show different color depending on the text in arrayList (eg, Color.Blue for "Normal", Color.Red for "Active", Color Gray for "Other"). In other words, when the listBox1 is populated, the following will be displayed-
    ABC Company - in blue
    DEF Company - in red
    GHI Company - in red
    JKL Comapny - in gray
    MNO Company - in blue

    QUESTION/HELP NEEDED:
    How do I code the listBox1_DrawItem event so that I can achieve the result as in (5) above?

    I've been researching the web but non has example similar to my problem. Any help/comment will be very much appreciated.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Using an array to hold the listbox items' status is not a great idea because if you added sorting to the listbox, the listbox items and array could become unsyncronised.

    I've gone about things a bit differently, using a simple object to hold the company name as well as it's status so that the 2 items are always together, thus avoiding any unwanted (and unnecessary) complications:

    Code:
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                listBox1.DrawMode = DrawMode.OwnerDrawFixed;
                listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
    
                listBox1.Items.Add(new MyObject("ABC Company", Status.Normal));
                listBox1.Items.Add(new MyObject("DEF Company", Status.Active));
                listBox1.Items.Add(new MyObject("GHI Company", Status.Active));
                listBox1.Items.Add(new MyObject("JKL Company", Status.Other));
                listBox1.Items.Add(new MyObject("MNO Company", Status.Normal));
            }
    
            private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
            {
                Color fore;
                MyObject obj = (MyObject)this.listBox1.Items[e.Index];
    
                switch (obj.status)
                {
                    case Status.Active: // active
                        fore = Color.Red;
                        break;
    
                    case Status.Normal: // normal
                        fore = Color.Blue;
                        break;
    
                    default: // other
                        fore = Color.Gray;
                        break;
                }
    
                e.Graphics.DrawString(obj.company, e.Font, new SolidBrush(fore), new PointF(e.Bounds.X, e.Bounds.Y));
            }
        }
    
        public enum Status
        {
            Active = 0,
            Normal = 1,
            Other = 2
        }
    
        class MyObject
        {
            public Status status;
            public String company;
    
            public MyObject() { }
    
            public MyObject(String company, Status status)
            {
                this.company = company;
                this.status = status;
            }
        }
    }
    Last edited by theoobe; 06-09-2009 at 07:28 PM.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    3
    Thanks for your timely help!
    Your example work fine, but the listBox1 now refuse to react to mouse click an focus event. What do I need to do so that I can intercept the focus and click event in this case?

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Smile

    Further to my earlier post, I am now able to access the focus and click events by slight modification to your example code!
    Once again, thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Variables already defined while linking.
    By xconspirisist in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2005, 06:20 AM
  4. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 04:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21