CTreeCtrl question

Posted on 2006-05-04
Last Modified: 2013-11-20
I'm using a CTreeCtrl populated from DB values to display a hiearchy  (user--reports---updates)

Populating it is working just fine, I select all users, loop through selecting all reports, looping through selecting all updates.

Within each loop I track the parent and add the items accordingly. For each line item, I am using SetItemData(<HTREEITEM>, ID)   -  ID being the recordset ID for the line item.

This all works. Currently however, I'm manually finding the level of each item, and simply hard-coding that "level 1 refers to users"  .. "level 2 refers to reports" .. and "level 3 refers to updates" - and that works as well. I know that an ID on level 2 is the ID of a record from the reports table, and ID on level 3 refers to a record from the updates table etc.

that seems "inappropraite" though - the hard-wiring of that. I have classes modeling each of those tables (lets call them MyUser, MyReport, MyUpdate) and rather than having to rely on "knowing" what each level refers to, I'd like to somehow have each line item in the tree "know" not only the ID, but the "what" the ID is for.

Does that make sense? Can I define some kind of structure which holds a DWORD ID and some kind of string identifier for the type of class I want to instantiate based on the ID of the selection and somehow stuff that into the SetItemData(..) call ?

Question by:PMH4514
    LVL 55

    Accepted Solution

    There is a simple aproach, I think:
    divide the ID in 2 parts:

    8 higher bits: item type (1=user, 2=report, 3=updates, etc)
    24 lower bits: ID itself

    So, every time you retrieve item's data (32 bits), evaluate the higher 8 bits (itemData>>24) to know the item type, and use only 24 bits (itemData & 0xFFFFFF) to identify the item.

    Author Comment

    interesting.. but that would still force me to know that 1==user and 2==report and 3==updates etc..  could I somehow stuff CMyUser as a class name to instantiate, into it? or am I overthinking unnecessarilly?
    LVL 55

    Expert Comment

    by:Jaime Olivares
    you can derive all these classes from a single abstract class (like CMyBase), so all can have equivalent functions you can call without knowing exactly the class type, or you can have a helper class (CMyClassManager) that evaluate the item type before calling proper function with a switch/case.
    LVL 55

    Expert Comment

    by:Jaime Olivares

    Author Comment

    I actually already use a factory pattern to instantiate these derivations of a common base class elsewhere in the app. I was primarilly looking for a way to stuff into each tree line item the value needed to supply to the factory. I think your first comment will work nicely.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now