Add Items to a Tree Control

Submitted by date of submission user level 

Mahesh Chand Feb 02, 2001 Beginner 

If you have never worked with a Tree Control then this article is for you. This article explains how to add tree items to a tree control. After writing couple of lines of code your tree control will look like this:

Add a Tree Control from your Controls to the dialog and add a member corresponding to this tree control, say m_TreeCtrl.

Now write this code. 

long lIdx, lIdx1 = 0;

// Add Items to the Tree Control

HTREEITEM hRoot, hCarItem, hTruckItem, hPlantItem ;

hRoot = m_TreeCtrl.InsertItem ("Root", 0, 0, TVI_ROOT, TVI_SORT);

hCarItem = m_TreeCtrl.InsertItem( "Car", 0, 0, hRoot, TVI_SORT );

hTruckItem = m_TreeCtrl.InsertItem( "Tempa Bay", 0, 0, hRoot, TVI_SORT ); 

hPlantItem = m_TreeCtrl.InsertItem( "Test", 0, 0, hRoot, TVI_SORT );

m_TreeCtrl.InsertItem( "Honda", 0, 0, hCarItem, TVI_SORT );

m_TreeCtrl.InsertItem( "Ford", 0, 0, hCarItem, TVI_SORT );

m_TreeCtrl.InsertItem( "Toyota", 0, 0, hCarItem, TVI_SORT );

m_TreeCtrl.InsertItem( "Honda", 0, 0, hRoot, TVI_SORT );

m_TreeCtrl.InsertItem( "Ford", 0, 0, hRoot, TVI_SORT );

m_TreeCtrl.InsertItem( "Toyota", 0, 0, hRoot, TVI_SORT );

m_TreeCtrl.InsertItem( "Dodge", 0, 0, hTruckItem, TVI_SORT ); 

m_TreeCtrl.InsertItem( "Ford", 0, 0, hTruckItem, TVI_SORT );

m_TreeCtrl.InsertItem( "Toyota", 0, 0, hTruckItem, TVI_SORT );

m_TreeCtrl.InsertItem( "Power", 0, 0, hPlantItem, TVI_SORT ); 

m_TreeCtrl.InsertItem( "Construction", 0, 0, hPlantItem, TVI_SORT );

m_TreeCtrl.InsertItem( "Gas", 0, 0, hPlantItem, TVI_SORT ); 

 

 

 

About the Author:

Mahesh is Admin and the founder of this site. He has been programming in VC++, Visual Basic, COM, ATL, Database Programming for 4 years. He can be reached at Mahesh. His  background includes Master's in Computer Science and Applications and BS in Mathematics and Physics.