![]() |
Desktop Development »
Tree Controls »
Custom Tree Controls
Intermediate
CFolderTreeCtrl class, or how to select folders and subfolders from a driveBy Adrien PinetAn article on how to select folders and subfolders in CTreeCtrl. |
VC6WinXP, MFC, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||




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.
This class allows to select folders and subfolders, with three rules:
Step 1: Add CFolderTreeCtrl files and icons.
Add CFolderTreeCtrl.h and CFolderTreeCtrl.cpp files in your project. Add the icons like below:
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:


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
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.
This class allows to retrieve selected folders from CFolderTreeCtrl to the FolderList, with the rule:
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"
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[])
treectrl.
CFolderList <=> INI File and CFolderTreeCtrl <=> CFolderList.
CFolderTreeCtrl can select a folder without clicking on checkbox, with SelectFolder(char folderPathName[]).
CFolderTreeCtrl, it will be auto-updated in the tree.
General
News
Question
Answer
Joke
Rant
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 |