Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#
Article

ControlExtender Library

Rate me:
Please Sign up or sign in to vote.
4.63/5 (12 votes)
5 Jul 20032 min read 110.4K   1.5K   49   12
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:

C#
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:

C#
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:

C#
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:

C#
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

C#
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


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

Comments and Discussions

 
QuestionExtending a listbox with a progressBar Control Pin
eRRaTuM17-May-06 5:41
eRRaTuM17-May-06 5:41 
Generalerror Pin
juli80243-May-05 22:15
juli80243-May-05 22:15 
QuestionHow to use the LIstViewExEdit Pin
Anonymous13-Apr-05 23:01
Anonymous13-Apr-05 23:01 
AnswerRe: How to use the LIstViewExEdit Pin
Reto Ravasio15-Apr-05 8:50
Reto Ravasio15-Apr-05 8:50 
GeneralRe: How to use the LIstViewExEdit Pin
juli802416-Apr-05 4:11
juli802416-Apr-05 4:11 
GeneralRe: How to use the LIstViewExEdit Pin
Reto Ravasio17-Apr-05 4:33
Reto Ravasio17-Apr-05 4:33 
GeneralRe: How to use the LIstViewExEdit Pin
juli802418-Apr-05 21:48
juli802418-Apr-05 21:48 
GeneralAscending Descending Arrow Pin
shakhshir3-Apr-05 5:51
shakhshir3-Apr-05 5:51 
hay man,

I'm creating my own Control that has a listview and your article helped me a lot. All i want to do is to show the Asc, Desc arrow.
Frankly I'm not a great Win API developer so all i did is i copied the code in your ListBoxExSort and all the methods that your code goes into while running and adjusted it according to my control. the code is working properly that the column gets shaded when clicking on top but still the arrow is not showing. I'm really in need for this please send me anything that can help me.
i searched the .resx files with no use. All i'm asking is Are you using images for arrows or drawing it at runtime.
please reply me ASAP.
thank you.
GeneralRe: Ascending Descending Arrow Pin
Reto Ravasio3-Apr-05 12:00
Reto Ravasio3-Apr-05 12:00 
GeneralProgress Bar in ListView Pin
Armoghan Asif21-Sep-03 4:17
Armoghan Asif21-Sep-03 4:17 
GeneralHideSelection Bug Pin
Yoeri12-Jul-03 22:42
Yoeri12-Jul-03 22:42 
GeneralRe: HideSelection Bug Pin
Reto Ravasio14-Jul-03 8:25
Reto Ravasio14-Jul-03 8:25 

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.