SortChildrenCB Problems

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!
LVL 4
SkonenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_mb_Commented:
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
SkonenAuthor Commented:
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
SkonenAuthor Commented:
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
_mb_Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nonubikCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.