Click here to Skip to main content
13,288,520 members (66,665 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 24 Jan 2002

CTreeCtrl\CListCtrl\CListBox With ToolTip Based On the Item Data

, 24 Jan 2002
Rate this:
Please Sign up or sign in to vote.
A CTreeCtrl derived class providing tooltips based on item data

Sample Image


Let's say you using  a ctrl that support item data (CTreeView for example) and you wish to add tool tip support , well,you can find some good articles explaining you exactly what to do ( look in the treeview section in code guru, Zafir Anjum's article will give you all the information you need. But, what if you want to display a custom tool tip for each item? One which is not based only on the item text? Well, I've searched for a class which will give me the freedom to display any tool tip I want and couldn't find any .

The following method will allow you to display an item tool tip ,which you will be composed according to the item data , so you can ,for example show a tool tip with a phone number when your mouse is over a name in a list ctrl .

The methods will be shown through the use of a CTreeCtrl derived class , but can be used with other controllers that support  set\get item data. This article will not focus on the technical sides of adding tool tip support to your controller. 

I would like to thank Yaniv ben ari for his great idea .

What is this Class ?

CTreeCtrlCh is a CTreeCtrl derived class , which display tool tip according to the item data You can attach any string you like to the any tree item using SetItemData function , and this text will be displayed as tool tip

How does this class work?

This class uses an abstract class as an ItemData templete. The user must derived it's own ItemData class from the abstract class ,and must implement a virtual function called: GetToolTipString , this function returns a CString object which will be displayed as tool tip . the user should construct this returned CString object according to his Own ItemData class. sounds a little complicated ? it's not...follow me to next section

How To Use This Class?

  • Add the files you've downloaded to your project directory and add them to your project.

  • Add a TreeCtrl to your dialog, and assign him a member using the control wizard

  • Add the following statment to your dialog header file:

  • Change the tree control variable type to CTreeCtrlch

  • In the dialog header file construct a new  class derived from
    as public. This  class will be uses as your tree item data , add any data member you want. Also add the function
    to that class.
    class TTipItemData:public ItemDataABSTRACT
    	CString strTool;
    	POSITION pos;
    	CString GetToolTipString();
  • in your dialog implementation file ,you need to implement the GetToolTipString() function This function MUST return a CString object, this object will be displayed as the item tool tip In this function you can manipulate the item data members and construct any string you want like additional information on the item, dates, size and etc .
    	strTool = "";
    	pos = NULL;
    CString TTipItemData::GetToolTipString() //here you compose the tool tip according to item data 
    	CString tmp;
    	return strTool;
  • when you to add item data , declare a pointer to the class you've derived from ItemDataABSTRACT assign data to it's member, and use SetItemData to add the ItemData pointer.
    TTipItemData* p ; 
    p = new TTipItemData;
    p->strTool = "is ran";
    level1= m_Tree.InsertItem("");
    m_Tree.SetItemText(level1,"First Name");
  • There is no need to delete the memory allocated, the CTreeCtrlch will free all allocated memory used for item data


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

ran wainstein
Web Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralHaving problems getting this to work Pin
vachaun24-Jul-08 6:08
membervachaun24-Jul-08 6:08 
Generalmore detail Pin
supfar31-Oct-07 17:05
membersupfar31-Oct-07 17:05 
Generalx64 Pin
Chmely9-Nov-06 2:04
memberChmely9-Nov-06 2:04 
GeneralI have the same needness about MultiLine tips Pin
HarveyLiu4-Jul-04 18:46
memberHarveyLiu4-Jul-04 18:46 
GeneralRe: I have the same needness about MultiLine tips Pin
Krangla14-Sep-04 5:20
sussKrangla14-Sep-04 5:20 
GeneralRe: I have the same needness about MultiLine tips Pin
venkat920028-Aug-07 5:38
membervenkat920028-Aug-07 5:38 
GeneralErrors if want buffer > 80 Pin
l_d_allan5-Jan-04 13:33
memberl_d_allan5-Jan-04 13:33 
I am very interested in using this control. Thanks for providing it.

I would like to use a tooltip greater than 80 characters. Whenever I modify the hard-coded 80 to be greater, it crashes. I couldn't find anything that specified the buffer size.

GeneralRe: Errors if want buffer > 80 Pin
Samuele1-Jul-04 6:21
memberSamuele1-Jul-04 6:21 
GeneralMemory Leak Pin
kknudsen26-Jun-03 5:42
memberkknudsen26-Jun-03 5:42 
GeneralRe: Memory Leak Pin
RayCool10-Jul-03 2:13
memberRayCool10-Jul-03 2:13 
GeneralIn Place Tooltip Pin
PoloM124-Apr-03 7:36
memberPoloM124-Apr-03 7:36 
mister trunks11-Dec-02 11:31
membermister trunks11-Dec-02 11:31 
GeneralProgram Error!! Pin
barquing4-Dec-02 2:09
sussbarquing4-Dec-02 2:09 
GeneralThis does not work for a tree control added in a class derived from CView Pin
Anonymous14-Oct-02 3:18
sussAnonymous14-Oct-02 3:18 
GeneralA few questions Pin
Thomas Freudenberg28-Feb-02 9:17
memberThomas Freudenberg28-Feb-02 9:17 
GeneralRe: A few questions Pin
Anonymous2-Mar-02 13:15
memberAnonymous2-Mar-02 13:15 
GeneralDowload links do not work... Pin
Nicolas Loiselet25-Jan-02 9:50
memberNicolas Loiselet25-Jan-02 9:50 
GeneralRe: Dowload links do not work... Pin
shitcoder27-Sep-05 21:26
membershitcoder27-Sep-05 21:26 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 25 Jan 2002
Article Copyright 2002 by ran wainstein
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid