Click here to Skip to main content
15,868,419 members
Articles / Programming Languages / C#
Tip/Trick

ListBox Color Item

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
25 Jan 2013CPOL 21.7K   5   1
User control to color items on a listbox.

Introduction

This code give the user the option to color items on listBox

Using the code

Simply download the DLL file and in on your project, go to the toolbox, right click on any control and select: Choose Items. Then on the tab: .NET Framework Components, select Browse, and then add the DLL file from the hard disk, click OK. Then just drag the control from the toolbox to your designer. The new control name is ListBoxControl1.

There are two properties: ChangeColor and MyListBox.

ChangeColor is to set/change a new color. And MyListBox contain all the properties of the listbox in the control.

When running the program if you don't add any items to the listbox by default it will show 10 items with the word Test near it. And if you don't change/set to any other color the default color will be Red.

Using the mouse left click button on any item won't do anything. Right clicking on any item will color it with the chosen color. Left clicking on a colored item will change it back to the original color.

C#
/*----------------------------------------------------------------
 * Module Name  : ListBoxControl
 * Description  : Change listBox items color
 * Author       : Danny
 * Date         : 30/12/2012
 * Revision     : 1.00
 * --------------------------------------------------------------*/


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

/*
 *  Introduction :
 * 
 *  By default the color is red.
 *  Added a property to change the color.
 *  Right mouse click on item to change item color.
 *  Left mouse click on item to change the item color back.
 *  If the listBox is empty the control will be filled with 10 "Test" items.
 * */

namespace ListBoxControl
{
    public partial class ListBoxControl1 : UserControl
    {
        private Color color;
        private List<int> m_itemIndexes = new List<int>();

        public ListBoxControl1()
        {
            InitializeComponent();

            if (listBox1.Items.Count == 0)
            {
                for (int i = 0; i < 10; i++)
                {
                    listBox1.Items.Add("Test " + i);
                }
            }
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            int index = listBox1.IndexFromPoint(e.X, e.Y);
            listBox1.SelectedIndex = index;

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (m_itemIndexes.Contains(index))
                    return;

                m_itemIndexes.Add(index);
                DrawItem(index);
            }
            else if (e.Button == MouseButtons.Left)
            {
                if (!m_itemIndexes.Contains(index))
                    return;

                m_itemIndexes.Remove(index);
                DrawItem(index);
            }
        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            color = ChangeColor;
            if (color.IsEmpty == true)
            {
                color = Color.Red;
            }
            bool coloring = m_itemIndexes.Contains(e.Index);
            bool selected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;

            if (coloring)
            {
                using (var brush = new SolidBrush(color))
                {
                    e.Graphics.FillRectangle(brush, e.Bounds);
                }
            }
            else
            {
                e.DrawBackground();
            }

            string item = listBox1.Items[e.Index].ToString();
            e.Graphics.DrawString(item, e.Font, selected || coloring ? 
              Brushes.White : Brushes.Black, e.Bounds, StringFormat.GenericDefault);

            if (selected)
                e.DrawFocusRectangle();
        }

        private void DrawItem(int index)
        {
            Rectangle rectItem = listBox1.GetItemRectangle(index);
            listBox1.Invalidate(rectItem);
        }

        [Browsable(true)]
        public Color ChangeColor
        {
            get { return color; }
            set
            {
                color = value;
                Refresh();
            }
        }

        [Browsable(true)]
        public ListBox MyListBox
        {
            get { return listBox1; }
            set
            {
                listBox1 = value;
                Refresh();
            }
        }
        private void UserControl1_Load(object sender, EventArgs e)
        {
            this.listBox1.SelectedIndex = 0;
        }
    }
}

You can download the DLL from this link: https://skydrive.live.com/redir?resid=3B8A7D9F66FF985B!203&authkey=!ANWjTxYfWmZ1iZw

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
vivek51129-Jan-13 20:29
vivek51129-Jan-13 20:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.