[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SortChildrenCB Problems

Posted on 2004-11-24
5
Medium Priority
?
946 Views
Last Modified: 2013-11-20
I'm having a very pecular problem when dealing with CTreeCtrl's SortChildrenCB function, when the function is executed the first two parameters are always equal to 0. However, the final parameter passed is the correct address for my CTreeCtrl class variable. I've looked all over to try to find a solution, but the whole issue doesn't make any sense to me, it's such a simple task yet something is going terribly wrong. Here is the implementation of my callback function:

static int CALLBACK FSCompare(LPARAM lParamFirst, LPARAM lParamSecond, LPARAM lParamSort) {
    // ...
}

And here is where I call the sorting function:

TVSORTCB tvs;

tvs.hParent = hSelectedItem;
tvs.lpfnCompare = FSCompare;
tvs.lParam = (LPARAM) this;

SortChildrenCB(&tvs);


hSelectedItem is a valid item and "this" is of course also valid. So if any one has any clue to what is going on here, please don't hesitate to pipe right up, this is a rather urgent matter... If a solution cannot be found, perhaps a suggestion for viable alternative will prove itself useful.

Thank you!
0
Comment
Question by:Skonen
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:_mb_
ID: 12663500
Hi,

if hSelectedItem don't have children, there are no items to sort and the parameters are 0.
To sort a group of items, tvs.hParent needs their parent.

To sort the whole tree (or a branch), you neeed a recursive procedure:

void <YourTreeCtrlClass>::SortTree(HTREEITEM hItem)
{
    if( !hItem )
    {
      m_treeListControl.SortItems(NULL);
      hItem = GetRootItem();
    }

    while( hItem )
    {
        if( ItemHasChildren(hItem) )
        {
            SortItems(hItem);
            SortTree(GetChildItem(hItem));
        }

        hItem = GetNextSiblingItem(hItem);
    }
}

void <YourTreeCtrlClass>::SortItems(HTREEITEM hItem)
{
   TVSORTCB tvs;
   tvs.hParent = hItem;    
   tvs.lpfnCompare = FSCompare;
   tvs.lParam = (LPARAM) this;

   SortChildrenCB(&tvs);
}  
0
 
LVL 4

Author Comment

by:Skonen
ID: 12668391
The parent does have children, which is why this is so frustrating. In fact for each child the sorting function is called, and each time the item parameters are 0.
0
 
LVL 4

Author Comment

by:Skonen
ID: 12670089
I found my rather embarrassing problem, I was using SetItemData with an item handle that wasn't yet initialized. Yet, I do have another question... I'm using the tree control with the TVS_CHECKBOXES style flag set, but I haven't found any messages that are sent when a checkbox state is changed, is there a way to be notified when such a change happens and then get a handle to the corresponding item?
0
 
LVL 3

Accepted Solution

by:
_mb_ earned 1000 total points
ID: 12672544
You can handle WM_LBUTTONDOWN (or/and other mouse click events you need) and test, where the event occured.

void ... ::OnLButtonDown(UINT nFlags, CPoint point)
{
    UINT      uiFlags;
    HTREEITEM hSelected = HitTest(point, &uiFlags);

    if( hSelected  && (uiFlags & TVHT_ONITEMSTATEICON) )
    {
        if( GetCheck(hSelected) )
        {
            // ...
        }          
    }
}
0
 
LVL 16

Expert Comment

by:nonubik
ID: 12673212
There is not such notification message, unfortunately.
Beside treating OnLButtonDown (as shown above) you'll also need to treat OnKeyDown for spacebar

void ::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      if( nChar == VK_SPACE )
      {
            HTREEITEM      hti = GetSelectedItem();
            int                  iCheck = GetCheck(hti);

      }

      CTreeCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

834 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