Click here to Skip to main content
Click here to Skip to main content

My Tree Control

By , 10 Nov 2005
 

Introduction

Many programmers rely on CTreeCtrl to help them view their applications. But the trouble they encounter is, always making sure that the tree is displaying their data correctly. This could be very annoying, writing it all in the dialog.

Background

Well, my tree isn't so nice looking, it really looks like an ordinary tree. But.. it stores items!! It is a template. If you are using my tree, the tree becomes the manager of the application instead of being managed.

If you haven't figured it yet, when you insert a new item, you also insert an item of your own you want to store. If it is a struct, let it be. If it is a class, that's fine too. If it is an abstract class pointed by a smart pointer, you made what really this tree is designed for. Beginners - I recommend reading about smart pointers, and reading about the Factory design pattern. I use "Loki" library for smart pointers.

My application uses this tree to store many items. Using an abstract class as the stored item, I specify what an item it is only when creating it.

Then, if I choose from the menu what action to do on an item, the right action happens depending on the item with in the tree. When an item is deleted, it destroys the item stored. You can specify in the destructor of an item what it should do on deletion. I use smart pointers, so my items are deleted only when that last item pointed to by the smart pointer is deleted. This design avoids unwanted annoying code, any code that makes adding new tree items complicated, and mostly making annoying stupid bugs.

Using the code

Simply create a tree control with the MFC wizard. Add a tree to a dialog, add a member. Include "myTreeCtrl.h" and change the type of the member to:

CMyTreeCtrl< UrItem> m_Tree

That's it! You have your new tree.

GetStorageItem would give you a pointer to the item inside. In addition, I added a very troublesome recursive code to get all items, all items by level, and a code that moves items - the first image you insert in CImageList should be an arrow of some sort, and also there is a code that transfers items to be children of other items.

There is something that sends messages on move, transfer and delete, but I don't use it in my main application. There is also a function to set the maximum hierarchy length.

All this code is based on the MFC example of CTreeCtrl. It uses STL to store items.

My tester application doesn't use all those features, most of them are in use in my commercial applications which aren't here. But it is simple to use.

I'd be happy to receive your comments. If you find a bug, I'd like to know about it.

Bunniez rulez.

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

About the Author

Amit Ziv
Web Developer
Israel Israel
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalgood idea, and waiting for XPTools, too!memberPhil. Invoker1-Nov-05 23:15 
Smile | :) Smile | :)
GeneralRe: good idea, and waiting for XPTools, too!memberAmitZiv10-Nov-05 11:53 
article was updated, XPTools Dll is included too.
now i believe my tree works without bugs.
 
too bad there is no example how to use the XPTools. Maybe if i get some extra time in the next millenium i would show some examples of XPTools. But don't worry, most of it is really simple. read my ReadMe.txt for help.
Only the tree here is mine, XPTools is a code i grabbed from many places and put them together. All of it is usefull except ListBoxXP.
QuestionXPTools?memberzabivan19-Oct-05 22:29 
Thanks for sharing your code!
 
I tried to compile it..
>Cannot open include file: '..\..\XPTools\Tools.h': No such file or directory
Do I miss something?
 

AnswerRe: XPTools?memberAmitZiv20-Oct-05 5:25 
i know, i saw it after i have published my code. I sent an update yesterday that contains the missing code, I hope they will re-publish it soon.
 
thanx for reporting!
GeneralNice idea !memberKochise19-Oct-05 21:22 
I'm currently developping myself some storage widgets that can automatically display their content Smile | :) So you don't have to mess with a Doc/View (storage/display) architecture anymore, all is done in the same interface...
 
Kochise
 
In Code we trust !
GeneralRe: Nice idea !memberAmitZiv20-Oct-05 5:39 
I'd rather say, in smart Abstract Factory Design i trust...
it's all written in the book: Design Patterns - Elements of Reusable Object-Oriented Software\ Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
It teaches you how to write an Application in a way you will never imagine yourself.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130617.1 | Last Updated 10 Nov 2005
Article Copyright 2005 by Amit Ziv
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid