Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ControlExtender Library

0.00/5 (No votes)
5 Jul 2003 1  
A component that enhances standard ListView and ListBox controls. (full designer support)

Sample Image - extendertest.png

Introduction

The control extender library contains three components:

  • ListViewExSort
    provides a ListView control with sorting capabilities.
  • ListViewExEdit
    provides a ListView control with editing capabilities.
  • ListBoxExEdit
    provides a ListBox control with editing capabilities.

To note here is that ListViewExSort shows the sort up/down arrows only under XP, because it uses functionality from comctrl version 6.0 which is only distributed with XP.

Background

From two projects I had two different classes which inherited from listview to extend its functionality. I felt that merging the two classes into one was not the right approach. Fortunately, I found the article ListViewSortManager control by Eddie Velasquez which gave me the idea not to inherit but to 'extend' listviews functionality.

Using the code

To use the extender controls, they have to be added to the toolbox first. The extenders can then be dropped onto a form where they will show up in the component tray. They can then be configured by setting the appropriate properties.

The image below shows the properties for ListViewExSort.

ListView Properties

The most important property is the control that the extender is associated with. This can also be coded manually like:

listViewExSort1.ListView=listView1;

The only property that can't be set with the designer is comparer. By default (if no comparers are provided) the columns will be sorted as a case-sensitive string. If sorting should be performed by something else, a custom sorter must be provided. The following code shows how to code a number comparer:

class Int32Comparer : IListViewComparer
{
    public int OnCompare(string x, string y)
    {
        try
        {
            return int.Parse(x, NumberStyles.Number) - 
                                int.Parse(y, NumberStyles.Number);
        }
        catch (System.Exception)
        {
            return -1;
        }
    }
}

The comparer class must be attached to the listview extender as follows:

listViewExSort1.Comparer=new IListViewComparer[]
   {
      null,    
      new Int32Comparer(),
   };

The sample above will sort the second column by number and all other columns by text.

Points of Interest

Key Navigation

What's not so obvious, is that ListBoxExEdit supports key-naviagtion. It is switched off by default. The following code enables it:

this.listBoxExEdit1.KeyNavigation = true;

Also note that the followging key-mappings are hardcoded into the library:

Ctrl-Insert - Insert a new line above the currently selected line
Ctrl-Delete - Delete the selected line
Ctrl-Down - Move the selected line down
Ctrl-Up - Move the selected line up

VS.NET

The sample project is for VS.NET 2003. Fortunately, there is a project available that converts 2003 files back to VS.NET.
A converted project will fail to compile on

Application.EnableVisualStyles(); // .NET 1.1 only

because EnableVisualStyles is only available in .NET 1.1. This line can be removed but the xp-look will then not be available.

Final Note

The control extender library is not yet ready to be used in all kinds of projects. Parts of it are hardly tested and I expect it to fail in ways I've never tested it before. For me, these three controls are useful and I intend to improve functionality and reliability as I work with them. I hope that there are some people out there that can live with the limited functionality and can give me feedback on how to improve.

History

  • Initial Release: July 6, 2003

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here