Click here to Skip to main content
6,290,315 members and growing! (16,721 online)
Email Password   helpLost your password?
Desktop Development » Tree Controls » Custom Tree Controls     Intermediate

CFolderTreeCtrl class, or how to select folders and subfolders from a drive

By Adrien Pinet

An article on how to select folders and subfolders in CTreeCtrl.
VC6WinXP, MFC, Dev
Posted:31 Oct 2004
Updated:21 Feb 2005
Views:130,720
Bookmarked:43 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
27 votes for this article.
Popularity: 6.45 Rating: 4.51 out of 5
1 vote, 3.7%
1
1 vote, 3.7%
2
3 votes, 11.1%
3
2 votes, 7.4%
4
20 votes, 74.1%
5

CFolderTreeCtrl1.jpg (75096 octets)

CFolderTreeCtrl2.jpg (24902 octets)

CFolderTreeCtrl3.jpg (31998 octets)

CFolderTreeCtrl4.jpg (27814 octets)

Introduction

If you want to select folders and subfolders from a drive in a CTreeCtrl, this article is for you. To use my class CFolderTreeCtrl and retrieve the selected folders, you must use the CFolderList class. CFolderTreeCtrl and CFolderList are completely free.

Using CFolderTreeCtrl

This class allows to select folders and subfolders, with three rules:

  • If a folder is selected, all subfolders are selected.
  • If all children of the current folder are selected, the current folder is also selected automatically.
  • If the current folder contains just one or multiple selected folders or subfolders, the current folder and all parents are bolded.

How to add CFolderTreeCtrl class in your project

Step 1: Add CFolderTreeCtrl files and icons.

Add CFolderTreeCtrl.h and CFolderTreeCtrl.cpp files in your project. Add the icons like below:

  • DRIVE_CDROM.ico
  • DRIVE_FIXED.ico
  • DRIVE_FLOPPY.ico
  • DRIVE_REMOVABLE.ico
  • FOLDER_CLOSE.ico
  • FOLDER_OPEN.ico

Step 2: Add CTreeCtrl control on your dialog window.

Select CTreeCtrl from the Controls floating toolbar, and create a bounding rectangle equal to the size of the list control you would like.

Step 3: Set the good property in your CTreeCtrl control like below:

CFolderTreeCtrl5.jpg (14659 octets)

CFolderTreeCtrl6.jpg (14935 octets)

Step 4: Add a CTreeCtrl instance.

Add a CTreeCtrl instance with class wizard, call the member variable m_tree by convention.

Step 5: Include CFolderTreeCtrl in the DIALOG_YOURDIALOG header (.h) file.

#include "CFolderTreeCtrl.h"

Step 6: Change CTreeCtrl instance to CFolderTreeCtrl in the DIALOG_YOURDIALOG header (.h) file.

//{{AFX_DATA(DIALOG_YOURDIALOG)

    enum { IDD = IDD_DIALOG_YOURDIALOG };
    CFolderTreeCtrl m_tree;
//}}AFX_DATA

How to use CFolderTreeCtrl class in your project

Just one step: Initialize the CFolderTreeCtrl instance m_tree:

void DIALOG_YOURDIALOG::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(DIALOG_FOLDERTREE)

    DDX_Control(pDX, IDC_TREE1, m_tree);
    //}}AFX_DATA_MAP


    m_tree.Init();
    m_tree.AddDrive("C:\\"); // or use m_tree.AddAllDrive();
}

You can also display the number of selected folders in use, m_tree.SetState, with a pointer to a CEdit control. To (pre-post)select a folder, use the SelectFolder method.

Now, your CFolderTreeCtrl is ready to use.

Using CFolderList

This class allows to retrieve selected folders from CFolderTreeCtrl to the FolderList, with the rule:

  • If a selected folder has already a selected parent, it will not be added in the list, and added otherwise.

How to add CFolderList class in your project

Step 1: Add CFolderList files in your project:

Add CFolderList.h and CFolderList.cpp files in your project.

Step 2: Include CFolderList in the DIALOG_YOURDIALOG declaration (.cpp) file.

#include "CFolderList.h"

How to use CFolderList class in your project

Step 1: Create an instance of CFolderList.

This construction method loads in the folder list, all selected folders from the m_tree instance of CTreeFolderCtrl.

CFolderList::FolderList(CFolderTreeCtrl* p_tree)

GetListSize method returns an integer. It represents the list size (the number of selected folders in the list).

int FolderList::GetListSize()

GetFolderPathName method returns a pointer of the folder string path at the position index. Returns NULL if the index is out of range.

char* FolderList::GetFolderPathName(int index)

AddFolderFromTree loads the selected folder from a CFolderTreeList in the current list.

void FolderList::AddFolderFromTree(CFolderTreeCtrl* p_tree)

AddFolderFromTree loads the selected item/folder from the current list in the CFolderTreeList.

void FolderList::AddFolderInTree(CFolderTreeCtrl* p_tree)

SaveListInFile saves the list folder in an INI file (returns 0 if an error has been detected).

int FolderList::SaveListInFile(char filePathName[])

LoadListInFile loads list folder from an INI file to the current fodlerList (returns 0 if an error has been detected).

int FolderList::LoadListFromFile(char filePathName[])

Update

  • 02/18/05
    1. CheckBox button style XP.
    2. Drive name displayed has root of the treectrl.
    3. Some optimisations and simplifications for the source code.
  • 01/02/05
    1. You can save and load selected folder between CFolderList <=> INI File and CFolderTreeCtrl <=> CFolderList.
    2. A right click button can display a context menu. It can open the selected folder and display all children selected.
  • 12/29/04
    1. Optimisation: Decreased (again) the complexity for count of selected folder(s).
    2. CFolderTreeCtrl can select a folder without clicking on checkbox, with SelectFolder(char folderPathName[]).
    3. If a folder is created after the display of the CFolderTreeCtrl, it will be auto-updated in the tree.
  • 11/08/04
    1. Optimisation: Decreased the complexity for count of selected folder(s).
  • 11/03/04
    1. Now you can auto-display the number of selected folder(s).
    2. Some optimisations for the architecture's class (no need to create event).

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

Adrien Pinet


Member
I'm a french student (21 years) in computer science.
I work on steganography and cryptography.
I program in visual c++ and QTDeigner under linux.

visit me : http://securengine.isecurelabs.com

Location: France France

Other popular Tree Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 88 (Total in Forum: 88) (Refresh)FirstPrevNext
GeneralWorking on KeyDown Function Pinmember"_$h@nky_"2:16 12 Aug '08  
QuestionNew Version?? Pinmembersunrisemxx22:49 14 Oct '07  
QuestionRight Icons?! ---> .AddAllDrive() Pinmemberrootdial9:41 22 Aug '07  
AnswerRe: Right Icons?! ---> .AddAllDrive() Pinmemberjhwurmbach5:00 14 Jul '08  
GeneralHow to get tree selection? Pinmember[ fade ]13:15 3 Nov '06  
GeneralCan you help me ! PinmemberNguyen Huu Thai19:29 17 Aug '06  
QuestionTrouble accessing third level from root PinmemberNickConnors14:09 19 Jun '06  
GeneralIsValidFolder PinmemberLars Wilke16:01 24 Apr '06  
GeneralKeyboard input PinmemberEspen Østrem23:47 2 Jan '06  
GeneralRe: Keyboard input PinmemberThomas Lerman4:41 21 Feb '06  
QuestionHow-to Select files? Pinmemberroxenhall22:48 10 Nov '05  
GeneralMemory leak PinsussRolfBe4:36 14 Jul '05  
GeneralRe: Memory leak PinmemberLars [Large] Werner6:44 31 Jan '06  
NewsRe: Memory leak - corrected fix code PinmemberDouglas R. Keesler14:10 29 Dec '07  
GeneralRe: Memory leak - corrected fix code Pinmembergolocy1:27 9 Apr '09  
GeneralHow we disable tree item PinmemberEvan Lin23:19 27 Jun '05  
GeneralWorking on .Net 2003? Pinsussvawksel13:36 4 May '05  
GeneralRe: Working on .Net 2003? Pinsussvawksel13:26 9 May '05  
GeneralRe: Working on .Net 2003?/2005 [modified] PinmemberTim Stubbs23:04 20 Feb '08  
GeneralCan't select parent alone... bad behavior? Pinsussvawksel13:16 4 May '05  
GeneralRe: Can't select parent alone... bad behavior? Pinsussvawksel13:13 12 May '05  
Generalsuper,,, but... PinmemberSataron22:52 29 Apr '05  
GeneralRe: super,,, but... PinmemberAdrien Pinet11:42 3 May '05  
GeneralMasks and List Pinmemberalex__b11:09 14 Mar '05  
GeneralRe: Masks and List PinmemberAdrien Pinet11:39 3 May '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Feb 2005
Editor: Smitha Vijayan
Copyright 2004 by Adrien Pinet
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project