Click here to Skip to main content
12,893,107 members (60,633 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

Generalx64 Pin
Chmely9-Nov-06 1:04
memberChmely9-Nov-06 1:04 

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
Web02 | 2.8.170424.1 | Last Updated 25 Jan 2002
Article Copyright 2002 by ran wainstein
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid