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

 
QuestionHow to get only one selectable node Pin
TuomoV31-Oct-06 21:55
TuomoV31-Oct-06 21:55 
AnswerRe: How to get only one selectable node Pin
SimonCampbell31-Oct-06 21:57
SimonCampbell31-Oct-06 21:57 
GeneralRe: How to get only one selectable node Pin
TuomoV31-Oct-06 22:05
TuomoV31-Oct-06 22:05 
GeneralRe: How to get only one selectable node Pin
TuomoV31-Oct-06 23:16
TuomoV31-Oct-06 23:16 
GeneralRe: How to get only one selectable node Pin
cnieva22-Feb-07 14:29
cnieva22-Feb-07 14:29 
GeneralRe: How to get only one selectable node Pin
cagriarican8-Feb-07 1:07
cagriarican8-Feb-07 1:07 
GeneralRe: How to get only one selectable node Pin
cnieva22-Feb-07 15:20
cnieva22-Feb-07 15:20 
QuestionCan I have for Web version? Pin
KhinLLK12-Sep-06 18:11
KhinLLK12-Sep-06 18:11 
Hi,

Sounds pretty good.
Can I have the web version for this control?
I want to use it in asp.net 1.0
Thanks
GeneralCheckbox's in treeview Pin
syonin8-Sep-06 7:09
syonin8-Sep-06 7:09 
GeneralDynamically showing textbox on click in cell Pin
ashubhalerao28-Aug-06 1:53
ashubhalerao28-Aug-06 1:53 
Generalrecycle bin Pin
syriast30-Jul-06 3:36
syriast30-Jul-06 3:36 
QuestionTo add focused node property in treelistview Pin
irfanahsan1-May-06 1:53
irfanahsan1-May-06 1:53 
AnswerRe: To add focused node property in treelistview Pin
SimonCampbell1-May-06 21:37
SimonCampbell1-May-06 21:37 
Generalvery good, well done Pin
walid100114-Apr-06 13:06
walid100114-Apr-06 13:06 
Questionhow to select complete row as one unit Pin
Rizwan Bashir16-Aug-05 23:00
Rizwan Bashir16-Aug-05 23:00 
AnswerRe: how to select complete row as one unit Pin
cnieva22-Feb-07 15:26
cnieva22-Feb-07 15:26 
GeneralRe: how to select complete row as one unit Pin
piolasoclau28-Dec-07 11:22
piolasoclau28-Dec-07 11:22 
GeneralRe: how to select complete row as one unit Pin
cnieva29-Dec-07 11:42
cnieva29-Dec-07 11:42 
GeneralRow Select Pin
TreyElliott3-Jan-05 3:18
TreyElliott3-Jan-05 3:18 
GeneralRe: Row Select Pin
cnieva22-Feb-07 15:34
cnieva22-Feb-07 15:34 
GeneralSlow Pin
Al_srd5-Jun-04 6:11
Al_srd5-Jun-04 6:11 
GeneralRe: Slow Pin
Micha_Kaz1-Nov-04 0:11
Micha_Kaz1-Nov-04 0:11 
GeneralPlease Help Me how use TreeListView Pin
Member 66765321-Dec-03 1:39
Member 66765321-Dec-03 1:39 

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.