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

Updated Extended ListView

Rate me:
Please Sign up or sign in to vote.
4.63/5 (24 votes)
30 Sep 20033 min read 361.1K   4.5K   97   146
An update to Jon Rista's treeview/listview control

Introduction

This posting is to update an existing posting by someone else (ContainerListView and TreeListView: Writing VS.NET design-surface compatible controls). The previous post by Jon Rista was a great foundation but inevitably, it had bugs. As Jon doesn't seem to be maintaining his post, I thought I'd help some of the people who originally downloaded his control, to be able to use a version that has the bugs I've found, fixed.

Please note that the code attached to this article has been modified using VS.NET 2003. If you want to use the code in the earlier version of VS.NET, you will need to create new .csproj and .sln files.

Why fix this control?

There are several other TreeView/ListView combo controls posted to Code Project, so why fix this one? The attraction of the control, to me, is that it is entirely in C#. We are developing an application and want to deliver a product that is all managed C# code. Most of the other implementations combine the existing TreeView and ListView controls and these, in our view, have three main draw backs:

  • They are COM, not managed code
  • The source is not available, so the appearance is determined by the authors (MS) not us
  • They were not designed to be used side-by-side

Another attraction is that Jon included hooks for the ability of the tree nodes to be composed of other objects. These might be a ListView with a different set of columns, another TreeView or a TextBox. Pretty visionary!

What has been fixed?

We noticed several bugs:

  1. The column titles were sized and displayed incorrectly (you can see this in the images presented in Jon's article
  2. The control Jon devised allows a user to include another control, however the painting of the embedded control was not always correct
  3. The mouse and keyboard movement had focus problems and, under some circumstances, the focus row would jump rows

Addressing these issues, especially the the mouse and keyboard event handling, has meant fairly substantial changes.

In his original article, Jon pointed out that the drawing routine called by the paint method was inefficient. This is because the routine computed the rows to show in the viewport by counting the rows each time. This is not an issue on a small or entirely collapsed list, but is an issue with larger lists. Addressing this issue has, again, meant fairly substantial changes. For example, the drawing routine is almost entirely rewritten and this was necessary because of the way the display size is now handled.

Now when a node is added or an existing node is expanded, collapsed or removed, its parents are notified and so they are able to maintain a note of the number of descendent nodes and the number of them that are expanded. The new drawing routine uses this additional information to workout what to draw and can do so more efficiently. The drawing routine, which is called often, is no longer affected by the length of the list as the work to maintain the node counts occurs as nodes are added, removed, expanded or collapsed.

While these changes will help performance, they can slow the process of creating the tree list in the first place. To address this, the control now exposes a BeginUpdate() method that can be called prior to adding any significant number of items. When the EndUpdate() method is called, the node values are recomputed and the control can be displayed.

Is this control now perfect?

Probably not. In fact in getting ready to post this article, I think I saw a bug that occurs when a call to ExpandAll() is used more than once. We intend to use this control (we've not done so yet), so please let me have a note of any issues you find. If you do report any problems, please make sure you tell me where to find the problem. If you have a program that illustrates the problem, please send it to me.

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
Web Developer
United Kingdom United Kingdom
Independent software development for clients including Microsoft. Design and development of modules for financial reporting and business intelligence.

Comments and Discussions

 
GeneralRe: Cannot compile the 2nd time Pin
Drew Noakes18-Mar-05 4:32
Drew Noakes18-Mar-05 4:32 
GeneralModifying Node Fonts Pin
dsb0219-Feb-04 6:18
dsb0219-Feb-04 6:18 
GeneralRight Click on the TreeListNode Pin
Prasad Papudesi5-Feb-04 5:18
Prasad Papudesi5-Feb-04 5:18 
GeneralRe: Right Click on the TreeListNode Pin
cchisho10-Mar-04 5:55
cchisho10-Mar-04 5:55 
GeneralRe: Right Click on the TreeListNode Pin
LaurMySelf23-Mar-04 5:16
LaurMySelf23-Mar-04 5:16 
GeneralScrollbar disappear when using thread to add item into list! Pin
paradisetux27-Jan-04 7:24
paradisetux27-Jan-04 7:24 
GeneralRe: Scrollbar disappear when using thread to add item into list! Pin
CoolDadTx29-Jan-04 10:44
CoolDadTx29-Jan-04 10:44 
GeneralChanges to control, anybody want to use them Pin
joshucar24-Jan-04 12:13
joshucar24-Jan-04 12:13 
I've taken the controls from the pair of Jon's ( codeproject.com/cs/miscctrl/extendedlistviews.asp ) and Bill's ( codeproject.com/cs/miscctrl/Extended_List_View_2.asp ) articles and have made many changes myself. My controls *should* do everything that these controls do, plus some. A brief list of changes I've made includes:

- Oodles of bug fixes and performance tweaks
- Combined the two controls, ContainerListView and TreeListView are now a single control ContainerListView. It's very easy to use the control as a basic ListView and just as trivial to turn it into a functional TreeView.
- Column re-ordering
- Column "hiding"
- Multiple Column Sorting (if you hold control while clicking on a column, it'll add that to the sort)
- Tool-Tips for column headers
- Multi-Line headers
- Each item can customize its own height, therefore not restricted to have the same height for every item
- Easy filtering (I don't have a "FilterBar", but a method is in place to filter the items. All you do is supply a filter to the control and it will limit the view to only show those items that belong.

I do plan on eventually writing a code project article and releasing the source code through here, but I wanted to add yet more features and continually test it to make it as perfect as possible. Notable future features I have in mind include:

- "Complex Headers" ala "Justified Form View" in MS Access (basically multiple rows of headers to define a complex item)
- A FilterBar
- Programmer supplied sorting, right now you must choose from a predifined list of types
- Copy and Paste functionality into HTML/Excel/etc.
- Row re-ordering via drag and drop
- Built in checkboxes and radio buttons, while you can achieve this with the contained controls, this seems to be used enough to allow for easier access to this functionality
- Column spanning (to allow for text to span into other columns)

I'm only able to spend a few hours here or there to work on this, so progress is a lot slower than I hoped. The reason for this message is to hopefully get a few beta-testers and/or developers out there to help test and/or develop this control. The faster it's got some more features and feels completely stable the faster I'll get an article written up. Feel free to respond to this message or email me at josh@chegg.com

Thanks a lot Jon and Bill for starting these wonderful controls!

Josh Carlson
GeneralRe: Changes to control, anybody want to use them Pin
AECAEC26-Jan-04 18:03
AECAEC26-Jan-04 18:03 
GeneralRe: Changes to control, anybody want to use them Pin
joshucar3-Feb-04 5:59
joshucar3-Feb-04 5:59 
GeneralRe: Changes to control, anybody want to use them Pin
dsb0219-Feb-04 14:55
dsb0219-Feb-04 14:55 
GeneralRe: Changes to control, anybody want to use them Pin
fmaeseele19-Aug-04 23:15
fmaeseele19-Aug-04 23:15 
GeneralRe: Changes to control, anybody want to use them Pin
fmaeseele21-Aug-04 4:25
fmaeseele21-Aug-04 4:25 
GeneralRe: Changes to control, anybody want to use them Pin
Jay Shankar24-May-04 15:10
Jay Shankar24-May-04 15:10 
GeneralRe: Changes to control, anybody want to use them Pin
yborf24-May-04 23:30
yborf24-May-04 23:30 
GeneralRe: Changes to control, anybody want to use them Pin
Oliver Smit2-Jul-04 4:11
sussOliver Smit2-Jul-04 4:11 
GeneralRe: Changes to control, anybody want to use them Pin
Anteros6-Jul-04 11:49
Anteros6-Jul-04 11:49 
GeneralRe: Changes to control, anybody want to use them Pin
pmilin4-Aug-04 6:49
pmilin4-Aug-04 6:49 
GeneralCode on SourceForge (was Re: Changes to control, anybody want to use them) Pin
ChromeBoy27-Sep-04 10:54
ChromeBoy27-Sep-04 10:54 
GeneralRe: Code on SourceForge (was Re: Changes to control, anybody want to use them) Pin
pmilin8-Nov-04 10:09
pmilin8-Nov-04 10:09 
GeneralRe: Code on SourceForge (was Re: Changes to control, anybody want to use them) Pin
pmilin8-Nov-04 10:20
pmilin8-Nov-04 10:20 
Generalerror while compiling Pin
Prasad Papudesi21-Jan-04 9:18
Prasad Papudesi21-Jan-04 9:18 
General.NET 1.0 Version Pin
Hamid Qureshi11-Jan-04 7:12
Hamid Qureshi11-Jan-04 7:12 
GeneralRe: .NET 1.0 Version Pin
Bill Seddon11-Jan-04 7:29
Bill Seddon11-Jan-04 7:29 
GeneralRe: .NET 1.0 Version Pin
Hamid Qureshi11-Jan-04 8:31
Hamid Qureshi11-Jan-04 8:31 

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.