Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / Visual Basic
Article

VB.NET TreeListView

Rate me:
Please Sign up or sign in to vote.
4.62/5 (30 votes)
18 Dec 2003CPOL2 min read 244.5K   4.2K   90   52
A Treeview/Listview hybrid, inheriting from Control, all custom drawn...

Introduction

Firstly, I'd like to mention that this code is still in its infancy, so you might find the odd (!) bug, or something that is done in an unusual way!

If you DO find bugs, or have any comments about ways to improve, please don't hesitate to mail me.

Needing a Treeview that is very flexible, and easily customizable, I first tried to inherit from the standard TreeView control shipped with .NET. Immediately I came across several problems which I couldn't find a satisfactory work-around to, which led me to look into the feasibility of writing a TreeView which inherits from Control, and do all the painting myself. Well at first glance, it’s not too much of a problem - I started out by trying a custom drawn ListView, which was fairly straightforward, so how much more difficult can a TreeView be...!?

Using the code

Ok, well I wanted these classes to be as scalable as possible, so I first created:

VB.NET
Public MustInherit Class View
    Inherits Control

This View class handles the basic stuff that would be common across any tree/list type controls that inherit from it.

  • It maintains a collection of columns
  • It handles the drawing of the column headers, borders, etc.
  • It provides a Protected MustOverride Sub DrawItems(ByVal g As Graphics, ByVal r As Rectangle)
  • It has two variables TotalHeight and TotalWidth, which are used for setting scrollbars.
  • TotalHeight Must be maintained by inheritors in their DrawItems method.
  • TotalWidth Is maintained by the View class, because it knows about it's own collection of columns.

The best way for you to get to grips with it is to download it!

Points of Interest

I've had some fun doing this, and learnt a lot about how to paint a control myself. I've tried to avoid contrived code, and done my best to keep everything as pure as possible. I'm a big believer that if you have to do a quick & dirty fix, it'll come back and haunt you later...!

Strange as it may seem, I found Microsoft Paint extremely useful while coding this... It was so useful to be able to do a screen print, and then use Paint to zoom in and have a look at how accurate my Paint code was...!

Planned Updates

I'm hoping to add in the drawing of root lines, and also the ability to have icons on each node.

History

  • 4-Dec-03 - First release.
  • 18-Dec-03 - Zip updated.

License

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


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSelected node text Pin
aravindkottapalli20-Jun-12 19:50
aravindkottapalli20-Jun-12 19:50 
GeneralMy vote of 5 Pin
Member 442549513-Dec-10 0:21
Member 442549513-Dec-10 0:21 
QuestionNo images? Pin
Frederik768-Mar-09 6:57
Frederik768-Mar-09 6:57 
GeneralVB.NET Version Pin
Ted Osberg10-Jun-08 19:02
Ted Osberg10-Jun-08 19:02 
QuestionHow to create a tree view for more than one item in ASP.net [modified] Pin
sahi3-Feb-08 22:46
sahi3-Feb-08 22:46 
GeneralNode height property Pin
virtual.aussie23-Jan-08 7:09
virtual.aussie23-Jan-08 7:09 
GeneralLines of the Tree Pin
piolasoclau28-Dec-07 11:23
piolasoclau28-Dec-07 11:23 
GeneralSelectedNode trouble in vb.net Pin
Ebjora21-Jun-07 0:57
Ebjora21-Jun-07 0:57 
QuestionRe: SelectedNode trouble in vb.net Pin
Smokeywade22-Oct-07 4:12
Smokeywade22-Oct-07 4:12 
GeneralRe: SelectedNode trouble in vb.net Pin
piolasoclau13-Nov-07 6:40
piolasoclau13-Nov-07 6:40 
GeneralNew TreeListView coming soon Pin
Ted Osberg17-Jun-07 13:49
Ted Osberg17-Jun-07 13:49 
GeneralRe: New TreeListView coming soon Pin
twinsfan20-Nov-07 17:02
twinsfan20-Nov-07 17:02 
GeneralRe: New TreeListView coming soon Pin
irish809618-Dec-07 7:06
irish809618-Dec-07 7:06 
GeneralRe: New TreeListView coming soon Pin
chaiguy133719-Dec-07 5:37
chaiguy133719-Dec-07 5:37 
QuestionRe: New TreeListView coming soon Pin
cnieva17-Mar-08 8:55
cnieva17-Mar-08 8:55 
AnswerRe: New TreeListView coming soon Pin
Ted Osberg5-Jun-08 14:50
Ted Osberg5-Jun-08 14:50 
QuestionCompact Framework Anyone? Pin
y2khines15-May-07 12:46
y2khines15-May-07 12:46 
GeneralKudos Pin
Technorodent4-Mar-07 8:20
Technorodent4-Mar-07 8:20 
GeneralRe: Kudos Pin
cnieva12-Mar-07 5:19
cnieva12-Mar-07 5:19 
QuestionI cant get this working in VB2005 Pin
HelderPinto791-Mar-07 5:52
HelderPinto791-Mar-07 5:52 
AnswerRe: I cant get this working in VB2005 Pin
cnieva1-Mar-07 7:00
cnieva1-Mar-07 7:00 
AnswerRe: I cant get this working in VB2005 Pin
HelderPinto791-Mar-07 21:53
HelderPinto791-Mar-07 21:53 
GeneralRe: I cant get this working in VB2005 Pin
cnieva2-Mar-07 4:28
cnieva2-Mar-07 4:28 
GeneralRe: I cant get this working in VB2005 Pin
yusuf01107-Nov-07 3:46
yusuf01107-Nov-07 3:46 
GeneralRe: I cant get this working in VB2005 Pin
cnieva7-Nov-07 11:08
cnieva7-Nov-07 11:08 

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.