Click here to Skip to main content
11,706,979 members (51,203 online)
Click here to Skip to main content

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

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

Sample Image

Introduction

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: #include "TreeCtrlCh.h"

  • Change the tree control variable type to CTreeCtrlch

  • In the dialog header file construct a new  class derived from ItemDataABSTRACT as public. This  class will be uses as your tree item data , add any data member you want. Also add the function CString GetToolTipString to that class.
    class TTipItemData:public ItemDataABSTRACT
    {
    public:
    
    	TTipItemData();
    
    	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 .
    TTipItemData::TTipItemData() 
    {
    	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");
    m_Tree.SetItemData(level1,DWORD(p));
  • There is no need to delete the memory allocated, the CTreeCtrlch will free all allocated memory used for item data

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

Share

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 5:08
membervachaun24-Jul-08 5:08 
Generalmore detail Pin
supfar31-Oct-07 16:05
membersupfar31-Oct-07 16:05 
Generalx64 Pin
Chmely9-Nov-06 1:04
memberChmely9-Nov-06 1:04 
GeneralI have the same needness about MultiLine tips Pin
HarveyLiu4-Jul-04 17:46
memberHarveyLiu4-Jul-04 17:46 
GeneralRe: I have the same needness about MultiLine tips Pin
Krangla14-Sep-04 4:20
sussKrangla14-Sep-04 4:20 
GeneralRe: I have the same needness about MultiLine tips Pin
venkat920028-Aug-07 4:38
membervenkat920028-Aug-07 4:38 
GeneralErrors if want buffer > 80 Pin
l_d_allan5-Jan-04 12:33
memberl_d_allan5-Jan-04 12:33 
GeneralRe: Errors if want buffer > 80 Pin
Samuele1-Jul-04 5:21
memberSamuele1-Jul-04 5:21 
GeneralMemory Leak Pin
kknudsen26-Jun-03 4:42
memberkknudsen26-Jun-03 4:42 
GeneralRe: Memory Leak Pin
RayCool10-Jul-03 1:13
memberRayCool10-Jul-03 1:13 
GeneralIn Place Tooltip Pin
PoloM124-Apr-03 6:36
memberPoloM124-Apr-03 6:36 
GeneralMULTIPLE LINE TOOLTIP Pin
mister trunks11-Dec-02 10:31
membermister trunks11-Dec-02 10:31 
GeneralProgram Error!! Pin
barquing4-Dec-02 1:09
sussbarquing4-Dec-02 1:09 
GeneralThis does not work for a tree control added in a class derived from CView Pin
Anonymous14-Oct-02 2:18
sussAnonymous14-Oct-02 2:18 
GeneralA few questions Pin
Thomas Freudenberg28-Feb-02 8:17
memberThomas Freudenberg28-Feb-02 8:17 
GeneralRe: A few questions Pin
Anonymous2-Mar-02 12:15
memberAnonymous2-Mar-02 12:15 
GeneralDowload links do not work... Pin
Nicolas Loiselet25-Jan-02 8:50
memberNicolas Loiselet25-Jan-02 8:50 
GeneralRe: Dowload links do not work... Pin
shitcoder27-Sep-05 20:26
membershitcoder27-Sep-05 20:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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