Click here to Skip to main content
13,350,861 members (56,268 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 1 Jun 2010

Custom List Box Control with Enable/Disable Items Feature

, 2 Jun 2010
Rate this:
Please Sign up or sign in to vote.
C# custom Listbox control that has Enable/Disable items feature


I worked once on a case where I had to show on a System.Windows.Forms.ListBox control some items that end user cannot select, but still they have to be shown anyway since the end user was expecting them. The point is that System.Windows.Forms.ListBox does not provide such a feature, so I decided to create my own user control and share it.

Control at Runtime

Here are some snapshots of the control at run time: [You can download the sample project build and run it].


after disabling Item3 [with Index 2] we will get:


Now Item3 cannot be selected, we will do the same with Item5 [Index4] and get:


Enabling Item3 [Index2] will revert the item at its default state:


Using the Code

Using this control is fairly straightforward. It is used in the same way the built-in ListBox control is used.

private Netdev.Windows.Forms.ListBox listBox1; 
// Create a listbox object.
this.listBox1 = new Netdev.Windows.Forms.ListBox(); 
// Disable the item at the integer location index.
// Enable the item at the integer location index.
// We can also wire an event handler for Disabled items selection event.
// Whenever an end user tries to select a disabled item this event will be fired
this.listBox1.DisabledItemSelected += new System.EventHandler

Describing the Code

The core of the customization happens on the OnDrawItem method override. However the runtime will call this method only if DrawMode = DrawMode.OwnerDrawFixed; which is the default value of the control property at creation and a list of disabled Indices [items] as shown in this constructor.

public ListBox()
      DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
      disabledIndices = new DisabledIndexCollection(this);

DisabledIndexCollection: This is a custom collection that is identical to the one holding SelectedIndices property. This collection tracks the indices that are currently disabled in the list which helps OnDrawItem method override to display those items as grayed items and prevent the end user from selecting them.

Here is the code of OnDrawItem method:

protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
    if (DesignMode  && Items.Count == 0)
        if (e.Index == 0)
            // Draw Control name as an item is design mode.
            e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
            e.Graphics.DrawString(this.Name, e.Font, SystemBrushes.WindowText, e.Bounds);

    if (e.Index != ListBox.NoMatches)
        object item = this.Items[e.Index];
        // Get the text to display, this depends if the list is bound to data and 
        // DisplayMember, EnableFormating and FormatString are set.
        string displayValue = GetItemText(item);
        if (disabledIndices.Contains(e.Index))
            e.Graphics.FillRectangle(SystemBrushes.InactiveBorder, e.Bounds);
            e.Graphics.DrawString(displayValue, e.Font, SystemBrushes.GrayText, e.Bounds);
            if (SelectionMode == System.Windows.Forms.SelectionMode.None)
                e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
                e.Graphics.DrawString(displayValue, e.Font, 
			SystemBrushes.WindowText, e.Bounds);
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
                    e.Graphics.DrawString(displayValue, e.Font, 
			SystemBrushes.HighlightText, e.Bounds);
                    e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
                    e.Graphics.DrawString(displayValue, e.Font, 
			SystemBrushes.WindowText, e.Bounds);

If an item is held in DisabledIndices collection, then display it as grayed.

And finally make sure to intercept SelectedIndexChanged event and swallow it if the selected item is grayed by overriding OnSelectedIndexChanged method.

 protected override void OnSelectedIndexChanged(EventArgs e)
    // Retrieve new value of selected index.
    int currentSelectedIndex = SelectedIndex;
    List<int> selectedDisabledIndices = new List<int>();

    for (int i = 0; i < SelectedIndices.Count; i++)
        // unselect items that are already disabled.
        if (disabledIndices.Contains(SelectedIndices[i]))
    foreach (int index in selectedDisabledIndices)
        // Fire DisabledItemSelected event for each 
        // disabled item that has been selected.
        IndexEventArgs args = new IndexEventArgs(index);
        OnDisabledItemSelected(this, args);
    // if updated selected index is different than the 
    // original one then bubble up the event
    if (currentSelectedIndex == SelectedIndex)


  • Updated version - Fixed an issue related to the items display value when the list is bound to data, or DisplayMember, EnableFormating and FormatString are set


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


About the Author

Mokdes Hamid
Software Developer (Senior)
Canada Canada
I've been working with .Net since 2006, I use C# as my favorite programming language and I have a strong knowledge about .Net frameworks, Windows and Web development.

I'm interested in a broad range of technologies that include, ADO.Net, WinForms, WPF, Silverlight as well as Best practices in development and design patterns.

I hold currently the following certifications:
MCTS: .NET Framework 2.0 Web Applications
MCTS: .NET Framework 2.0 Windows Applications
MCTS: .NET Framework 2.0 Distributed Applications
MCPD: Designing and Developing Enterprise Applications by Using the Microsoft .NET

For more posts:
C# and Windows Form Programming

Walkthrough on how to create a Control Extender in C#

Control Anchoring Class

You may also be interested in...


Comments and Discussions

QuestionProblem with datatable Pin
Riccardo Paladino12-Nov-12 10:45
memberRiccardo Paladino12-Nov-12 10:45 
GeneralInteresting idea Pin
S.H.Bouwhuis7-Jun-10 22:54
memberS.H.Bouwhuis7-Jun-10 22:54 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 2 Jun 2010
Article Copyright 2010 by Mokdes Hamid
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid