Click here to Skip to main content
15,881,812 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 360.4K   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

 
QuestionIs there any license for using this code Pin
Member 32601956-Nov-14 20:01
Member 32601956-Nov-14 20:01 
QuestionListView Header Pin
kaka228-Nov-12 4:51
kaka228-Nov-12 4:51 
QuestionProblem with multiselect Pin
cs_CodeMaster24-Feb-12 1:45
cs_CodeMaster24-Feb-12 1:45 
QuestionAfterCollapse, AfterExpand, ProgressBar Pin
ntuanxb30-Aug-11 17:11
ntuanxb30-Aug-11 17:11 
GeneralHow to add check box in front of each node? Pin
Member 277113821-Apr-11 13:31
Member 277113821-Apr-11 13:31 
GeneralBug found using TextBox Pin
hugobicho23-Jun-10 10:20
hugobicho23-Jun-10 10:20 
Questionhow can i clear all nodes of the listview? Pin
raopeitao29-Oct-08 23:43
raopeitao29-Oct-08 23:43 
AnswerRe: how can i clear all nodes of the listview? Pin
ntuanxb31-Aug-11 22:34
ntuanxb31-Aug-11 22:34 
QuestionHow to get the bounds of a ContainerListViewItem? Pin
gewe21-Oct-08 0:43
gewe21-Oct-08 0:43 
QuestionThe Text in SubItems of a Node is drawn incorrectly Pin
kion211-Sep-08 17:31
kion211-Sep-08 17:31 
GeneralSome problems and approaches Pin
kion211-Sep-08 17:08
kion211-Sep-08 17:08 
GeneralRe: Some problems and approaches Pin
A. Raees7-Oct-09 4:46
A. Raees7-Oct-09 4:46 
GeneralVB.NET version Pin
Ted Osberg10-Jun-08 18:59
Ted Osberg10-Jun-08 18:59 
QuestionHow to populate the SmallImageList property Pin
naresh123422-Feb-08 20:26
naresh123422-Feb-08 20:26 
GeneralUpdated version available on sourceforge Pin
I'm Chris31-Oct-07 4:57
professionalI'm Chris31-Oct-07 4:57 
GeneralRe: Updated version available on sourceforge Pin
Bill Seddon31-Oct-07 6:30
Bill Seddon31-Oct-07 6:30 
Generalremove Item Pin
JossGP19-Oct-07 11:45
JossGP19-Oct-07 11:45 
Generaltreelist child node subitem checkbox control still display after collapse node Pin
nik4u20-Sep-07 1:00
nik4u20-Sep-07 1:00 
QuestionHow to change color of subitem Pin
bengoan28-Mar-07 1:36
bengoan28-Mar-07 1:36 
AnswerRe: How to change color of subitem Pin
Bill Seddon4-Apr-07 22:19
Bill Seddon4-Apr-07 22:19 
GeneralBill Seddon's //cs/miscctrl/Extended_List_View_2.asp article Pin
Mons211015-Sep-06 4:24
Mons211015-Sep-06 4:24 
GeneralFix for items not properly deselected Pin
The_Mega_ZZTer2-Aug-05 16:27
The_Mega_ZZTer2-Aug-05 16:27 
If you select any item (except the first) in the listview and then delete it, SelectedIndices still has the item in it!

The fix is easy, fortunately. Open up ContainerListView.cs and find the SelectiveSelection function.

Since I ported the code to VB my code is a tad differently formatted, but there is a line that reads:

selectedindices.Remove(i);

or something like that. Change the Remove to a RemoveAt.

Done! Smile | :)

-- modified at 17:29 Tuesday 6th December, 2005
GeneralRe: Fix for items not properly deselected Pin
The_Mega_ZZTer6-Dec-05 11:27
The_Mega_ZZTer6-Dec-05 11:27 
GeneralMinor issue: Don't call Expand on already expanded nodes Pin
Michael B. Hansen7-Jul-05 2:22
Michael B. Hansen7-Jul-05 2:22 
GeneralRelated issue: If you add a new node... Pin
Michael B. Hansen8-Jul-05 0:02
Michael B. Hansen8-Jul-05 0:02 

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.